How do you get the sum of second and third values in a list of tuples grouped by the first value?
I.e:
list_of_tuples = [(1, 3, 1), (1, 2, 4), (2, 1, 0), (2, 2, 0)]
expected_output = [(1, 5, 5), (2, 3, 0)]
I found several great answers on StackOverflow doing this found tuples with two values, but couldn't figure out how to adjust them for summing both second and third values.
One of the good answers for just the second value was this:
def sum_pairs(pairs):
sums = {}
for pair in pairs:
sums.setdefault(pair[0], 0)
sums[pair[0]] += pair[1]
return sums.items()