This can be done in a single line without any imports using some clever reducing
To understand how this works, you have to understand how reduce works. Essentially, it allows you define an operation which takes two element from the list and reduces them down to a single element. It then applies this operation recursively to your list until the list has been reduced to a single element. Here is the one-line version:
dict1 = [{'a':1}, {'b':1}, {'a':1}, {'b':2}]
print(reduce(lambda a, b: {k: a.setdefault(k, 0) + b.setdefault(k, 0) for k in set(a.keys()).union(b.keys())}, dict1))
In this case, the operation is defined as:
lambda a, b:
{k: a.setdefault(k, 0) + b.setdefault(k, 0) for k in (set(a.keys()).union(b.keys()))}
Which can also be expressed as:
# a and b are two elements from the list. In this case they are dictionaries
def combine_dicts(a, b):
output = {}
for k in set(a.keys()).union(b.keys()): # the union of the keys in a and b
output[k] = a.setdefault(k, 0) + b.setdefault(k, 0)
# dict.setdefault returns the provided value if the key doesn't exist
return output
When this operation is applied to your list with reduce, you get the desired output:
>>> {'b': 3, 'a': 2}