How can I combine dictionaries with the same keys?

Solution 1:

big_dict = {}
for k in dicts[0]:
    big_dict[k] = [d[k] for d in dicts]

Or, with a dict comprehension:

{k: [d[k] for d in dicts] for k in dicts[0]}

Solution 2:

You can use collections.defaultdict. The benefit of this solution is it does not require keys to be consistent across dictionaries, and it still maintains the minimum O(n) time complexity.

from collections import defaultdict

dict_list = [{'key_a': 'valuex1', 'key_b': 'valuex2', 'key_c': 'valuex3'},
             {'key_a': 'valuey1', 'key_b': 'valuey2', 'key_c': 'valuey3'},
             {'key_a': 'valuez1', 'key_b': 'valuez2', 'key_c': 'valuez3'}]            

d = defaultdict(list)
for myd in dict_list:
    for k, v in myd.items():
        d[k].append(v)

Result:

print(d)

defaultdict(list,
            {'key_a': ['valuex1', 'valuey1', 'valuez1'],
             'key_b': ['valuex2', 'valuey2', 'valuez2'],
             'key_c': ['valuex3', 'valuey3', 'valuez3']})

Solution 3:

If all the dicts have the same set of keys, this will work:

dict((k, [d[k] for d in dictList]) for k in dictList[0])

If they may have different keys, you'll need to first built a set of keys by doing set unions on the keys of the various dicts:

allKeys = reduce(operator.or_, (set(d.keys()) for d in dictList), set())

Then you'll need to protect against missing keys in some dicts:

dict((k, [d[k] for d in [a, b] if k in d]) for k in allKeys)