I have a list of "tagged" tuples...where each tuple is (tag_id, value)...like so:
my_list = [(tag_A, 100), (tag_A, 200), (tag_A, 300), (tag_A, 400), (tag_B, 400), (tag_B, 600)]
I want to sum the values of each tuple with the same tag...so that:
sum_of_all_values_with_tag_A() = 1000
sum_of_all_values_with_tag_B() = 1000
I can't figure out a simple Pythonic way of doing that.
sum(set(value for tag_id, value in my_list))
...returns the sum of ALL the values.
I suppose I can wrap that with a for or a while loop, so that only tuples with the tag I want to sum are touched by that expression...? I need to sum the values associated with both tags...resulting in two different totals, differentiated as above. But can't quite grok an elegant syntax for such a thing.
This is happening inside of a pre-existing function. Would be great to do it without nesting functions.
Any suggestions are appreciated!