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
How about this: (assuming a
is the name of the tuple you have provided)
letters_to_numbers = {}
for i in a:
if i[0] in letters_to_numbers:
letters_to_numbers[i[0]] += i[1]
else:
letters_to_numbers[i[0]] = i[1]
b = letters_to_numbers.items()
The elements of the resulting tuple b
will be in no particular order.