How can I sum up dictionaries in a list (python)?
Solution 1:
You can use a temporary dictionary to hold the sum and recreate the original structure
from collections import defaultdict
item_list = [{'item': 'apple', 'amount': 200}, {'item': 'apple', 'amount': 150}, {'item': 'orange', 'amount': 300}]
sumdict = defaultdict(int)
for item in item_list:
sumdict[item['item']] += item['amount']
result = [{'item': k, 'amount': v} for k,v in sumdict.items()]
print(result)