How to combine dictionaries with the same keys in python?

Let's say I have a list of such dictionaries:

dict[0] is {'key_a': valuex1, 'key_b': valuex2, 'key_c': valuex3} dict[1] is {'key_a': valuey1, 'key_b': valuey2, 'key_c': valuey3} dict[2] is {'key_a': valuez1, 'key_b': valuez2, 'key_c': valuez3} 

I would like to take them and build a large dictionary, for example:

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

Is there some elegant zip -like way for me to do this?

All keys will always be identical.

I can always iterate over the keys on each and build a new dictionary of lists, but it seems very non-python.

+7
source share
5 answers
 big_dict = {} for k in dicts[0]: big_dict[k] = [d[k] for d in dicts] 

(I renamed your dict to dicts , since dict is inline, and dicts makes more sense.)

Or, with a dict understanding:

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

or, for Python <2.7:

 dict((k, [d[k] for d in dicts]) for k in dicts[0]) 
+10
source

If all 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 can have different keys, you first need to create a set of keys by combining the sets on the keys of different dicts:

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

Then you 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) 
+3
source

You can use collections.defaultdict . The advantage of this solution is that it does not require key consistency in different dictionaries, and at the same time it preserves the minimum time complexity O (n).

 from collections import defaultdict dictList = [{'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 dictList: 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']}) 
+1
source

You can combine the dictionaries as follows:

 def merge_dicts(dict_list, separator=''): """ Merges list of dictionaries to a single dictionary, Concatenates values with the same key. :param dict_list: list of dictionaries to be merged. :param separator: separator to be inserted between values of same key. :return: Merged dictionary. """ return {k1: separator.join([d[k1] for d in dict_list if k1 in d]) for k1 in set(reduce(lambda x, y: x+y, [k.keys() for k in dict_list])) } 
0
source

If you are happy to use a third-party library, you can use Pandas. The pd.DataFrame constructor takes a list of dictionaries directly:

 import pandas as pd res = pd.DataFrame(dictList).to_dict(orient='list') {'key_a': ['valuex1', 'valuey1', 'valuez1'], 'key_b': ['valuex2', 'valuey2', 'valuez2'], 'key_c': ['valuex3', 'valuey3', 'valuez3']} 
0
source

Source: https://habr.com/ru/post/920761/


All Articles