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
A one liner:
>>> x = [ ('A',100), ('B',50), ('A',50), ('B',20), ('C',10) ] >>> { ... k: reduce(lambda u, v: u + v, [y[1] for y in x if y[0] == k]) ... for k in [y[0] for y in x] ... }.items() [('A', 150), ('C', 10), ('B', 70)]