I\'ve looked through a few of the questions here and none of them seem to be exactly my problem. Say I have 2 dictionaries, and they are dict1
{\'A\': 25 ,
This is one way, using defaultdict:
# the setup
>>> from collections import defaultdict
>>> dict1 = {'A': 25, 'B': 41, 'C': 32}
>>> dict2 = {'A': 21, 'B': 12, 'C': 62}
# the preperation
>>> dicts = [dict1, dict2]
>>> final = defaultdict(list)
# the logic
>>> for k, v in ((k, v) for d in dicts for k, v in d.iteritems()):
final[k].append(v)
# the result
>>> final
defaultdict(<type 'list'>, {'A': [25, 21], 'C': [32, 62], 'B': [41, 12]})
I just needed to combine an unknown number dictionaries and after seeing answers to this question came up with more universal solutions:
1) dictionaries of values:
dic1 = {'A': 1, 'B': 1, 'C': 1}
dic2 = {'A': 2, 'B': 2, 'C': 2}
dic3 = {'D': 3, 'E': 3, 'F': 3}
def combineDictVal(*args):
result = {}
for dic in args:
for key in (result.viewkeys() | dic.keys()):
if key in dic:
result.setdefault(key, []).append(dic[key])
return result
print combineDictVal(dic1, dic2, dic3)
Result:
{'A': [1, 2], 'C': [1, 2], 'B': [1, 2], 'E': [3], 'D': [3], 'F': [3]}
2) dictionaries of lists (difference is in append/extend*):
dic1 = {'A': [1, 2], 'B': [1, 2], 'C': [1, 2]}
dic2 = {'A': [3, 4], 'B': [3, 4], 'C': [3, 4]}
dic3 = {'D': [5, 6], 'E': [5, 6], 'F': [5, 6]}
def combineDictList(*args):
result = {}
for dic in args:
for key in (result.viewkeys() | dic.keys()):
if key in dic:
result.setdefault(key, []).extend(dic[key])
return result
print combineDictList(dic1, dic2, dic3)
Result:
{'A': [1, 2, 3, 4], 'C': [1, 2, 3, 4], 'B': [1, 2, 3, 4], 'E': [5, 6], 'D': [5, 6], 'F': [5, 6]}
3) Universal for int / lists:
dic1 = {'A': 1, 'B': 1, 'C': [1, 1]}
dic2 = {'A': 2, 'B': [2, 2], 'C': 2}
dic3 = {'D': 3, 'E': 3, 'F': [3, 3]}
def combineDict(*args):
result = {}
for dic in args:
for key in (result.viewkeys() | dic.keys()):
if key in dic:
if type(dic[key]) is list:
result.setdefault(key, []).extend(dic[key])
else:
result.setdefault(key, []).append(dic[key])
return result
print combineDict(dic1, dic2, dic3)
Result:
{'A': [1, 2], 'C': [1, 1, 2], 'B': [1, 2, 2], 'E': [3], 'D': [3], 'F': [3, 3]}
These solutions are not the fastest possible, however they are clear and work just fine. Hope it will help someone.
Use pydash
from pydash import merge_with
cbk = lambda obj_val, src_val: obj_val + src_val
obj1 = {'a': [1], 'b': [2]}
obj2 = {'a': [3], 'b': [4]}
res = merge_with(obj1, obj2, cbk)
print(res)
Result:
{'a': [1, 3], 'b': [2, 4]}
you can also use dictlib
import dictlib
obj1 = {'a': [1], 'b': [2]}
obj2 = {'a': [3], 'b': [4],"c":[3]}
c = dictlib.union_setadd(obj1, obj2)
print(c)
{'a': [1, 3], 'b': [2, 4], 'c': [3]}
This is a generic version. This can be used to create a dictionary with values as a list, even if the key is present in only one of them.
dic1 = {'A': 25, 'B': 41, 'C': 32}
dic2 = {'A': 21, 'B': 12, 'C': 62}
result = {}
for key in (dic1.keys() | dic2.keys()):
if key in dic1: result.setdefault(key, []).append(dic1[key])
if key in dic2: result.setdefault(key, []).append(dic2[key])
print(result)
Output
{'A': [25, 21], 'C': [32, 62], 'B': [41, 12]}
If you are using Python 2, for loop has to be changed like this:
for key in (dic1.viewkeys() | dic2.keys()):
This is another way to do it, and will work regardless if the keys are present in only one or in both dictionaries.
def merge_dicts(dict_a, dict_b):
merged_dict = {key: [value] for key, value in dict_a.iteritems()}
for key, value in dict_a.iteritems():
try:
merged_dict[key].append(value)
except KeyError:
meeged_dict[key] = [value]
return ret_dict
def combineDict(dict1, dict2):
res = collections.defaultdict(list)
for key, value in (dict1.items() | dict2.items()):
res[key].append(value)
return res
print(combineDict(dict1, dict2))