I have a list of dictionaries, e.g:
dictList = [
{'a':3, 'b':9, 'c':4},
{'a':9, 'b':24, 'c':99},
{'a':10, 'b':23, 'c':88}
]
All the dictionaries have the same keys e.g. a, b, c. I wish to create a single dictionary with the same keys where the values are the sums of the values with the same keys from all the dictionaries in the original list.
So for the above example, the output should be:
{'a':22, 'b':56, 'c':191}
What would be the most efficient way of doing this? I currently have:
result = {}
for myDict in dictList:
for k in myDict:
result[k] = result.setdefault(k, 0) + myDict[k]