I have an arbitrary number of equal-length python dictionaries with matching sets of keys, like this:
{'a':1, 'b':4, 'c':8, 'd':9}
{'a':2, 'b':3, 'c':2, 'd':7}
{'a':0, 'b':1, 'c':3, 'd':4}
...
How can I obtain a single dictionary with the same set of keys but with values as the sums of corresponding elements in the dictionary set? In other words, I'd want:
{'a':3, 'b':8, 'c':13, 'd':20}
Maybe there's an ugly, complicated loop structure, but is there a nicer way to do this with some kind of list/dict comprehension cleverness? Come to think of it, I'm really not sure how to make an ugly loop version, anyway..
If you simply want to use
dict
only, you can use thisOutput:
collections.Counter()
to the rescue ;-)Then:
Or you can change it back to a dict:
It doesn't matter to
Counter()
whether all the input dicts have same keys. If you know for sure that they do, you could try ridiculous ;-) one-liners like this:Counter()
is clearer, faster, and more flexible. To be fair, though, this slightly less ridiculous one-liner is less ridiculous: