I have a list of tuples:
[ (\'A\',100), (\'B\',50), (\'A\',50), (\'B\',20), (\'C\',10) ]
I am trying to sum up all numbers that have the s
Try this:
a = [('A',100), ('B',50), ('A',50), ('B',20), ('C',10) ] letters = set([s[0] for s in a]) new_a = [] for l in letters: nums = [s[1] for s in a if s[0] == l] new_a.append((l, sum(nums))) print new_a
Results:
[('A', 150), ('C', 10), ('B', 70)]