11

I have two dictionaries and need to combine the values of similar keys in them. Here's an example:

dict1 = {'key1':[value11,value12,value13] , 'key2':[value21,value22,value23]}
dict2 = {'key1':[value14,value15] , 'key2':[value24,value25]}

I used :

dict3 = {}
for key in (dict1.viewkeys() | dict2.keys()):
    if key in dict1: dict3.setdefault(key, []).append(dict1[key])
    if key in dict2: dict3.setdefault(key, []).append(dict2[key])

which gives me:

dict3 = {'key1':[[value11,value12,value13],[value14,value15]] , 'key2':[[value21,value22,value23],[value24,value25]]}

What I want is a simple one like:

Desired output :

 dict3 = {'key1':[value11,value12,value13,value14,value15] , 'key2':[value21,value22,value23,value24,value25]}
Hypothetical Ninja
  • 3,920
  • 13
  • 49
  • 75

5 Answers5

3

All you need to do is to modify append to extend which will then add the elements of the list rather than adding the list itself. See the list docs for more details on the difference between append and extend.

dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}

dict3 = {}
for key in set().union(dict1, dict2):
    if key in dict1: dict3.setdefault(key, []).extend(dict1[key])
    if key in dict2: dict3.setdefault(key, []).extend(dict2[key])

print(dict3)
# {'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}

Alternatively you could use a collections.defaultdict with the default set to list as shown below.

from collections import defaultdict
dict3 = defaultdict(list)

for key in set().union(dict1, dict2):
  for dic in [dict1, dict2]:
    if key in dic:
      dict3[key] += dic[key]
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • what does defaultdict(list) do?? – Hypothetical Ninja Aug 20 '14 at 13:34
  • `defaultdict(list)` is similar to using `setdefault` in your example. Basically if the key isn't already in `dict3` then it will set the value as default `[]`. This way is just a bit neater (IMHO). – Ffisegydd Aug 20 '14 at 13:35
  • I dislike the except KeyError: pass − It's a bad management of Exceptions. – Quentin THEURET Aug 20 '14 at 13:36
  • @Ffisegydd you can use a condition (if key in dic) instead of an Exception management with a pass statement. – Quentin THEURET Aug 20 '14 at 13:43
  • Plus, I tried to test your answer and (dict1.keys() | dict2.keys()) returns an error in python 2.7 – Quentin THEURET Aug 20 '14 at 13:44
  • @QuentinTHEURET whilst I disagree with you I've modified it as it is probably more readable for people who aren't too versed in `try...except...` loops. And yes I changed it to `keys` for Python 3.4 (which is what I used). – Ffisegydd Aug 20 '14 at 13:45
3

Here is a generic method on which you can pass as many dict as you want as parameter.

>>> def mix_dict(*args):
       res = {}
       for d in args:
           if not isinstance(d, dict):
               continue
           for k, v in d.iteritems():
               res.setdefault(k, [])
               if isinstance(v, list):
                   res[k].extend(v)
               else:
                   res[k].append(v)
       return res
>>> dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
>>> dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
>>> dict3 = mix_dict(dict1, dict2)
>>> print dict3
... {'key1': ['value11', 'value12', 'value13', 'value14', 'value15'],
     'key2': ['value21', 'value22', 'value23', 'value24', 'value25']}
Quentin THEURET
  • 1,222
  • 6
  • 12
2

You can do it much simpler but if you want to use your code just change append to extend

dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}

dict3 = {}
for key in (dict1.viewkeys() | dict2.keys()):
    if key in dict1: dict3.setdefault(key, []).extend(dict1[key])
    if key in dict2: dict3.setdefault(key, []).extend(dict2[key])

print dict3

output:

{'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}

You can read this post about the difference ov append to extend

Community
  • 1
  • 1
Kobi K
  • 7,743
  • 6
  • 42
  • 86
1

Here is another way to do this.

You can support merging N dicts of lists into a single dict of lists with this function:

def mergeDoLs(*dicts):
    def flatten(LoL):
        return [e for l in LoL for e in l]

    rtr={k:[] for k in set(flatten(d.keys() for d in dicts))}
    for k, v in flatten(d.items() for d in dicts):
        rtr[k].extend(v)
    return rtr

To use:

>>> dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23'], 'key3':[1]}
>>> dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
>>> dict3 = {'key3':[2]}
>>> mergeDoLs(dict1, dict2, dict3) 
{'key3': [1, 2], 'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}
dawg
  • 98,345
  • 23
  • 131
  • 206
0

Use dict.update() to merge two dictionaries keys: dict1['key1'].update(dict2['key1'])

BSalita
  • 8,420
  • 10
  • 51
  • 68