1

Let us Assume, I have a key value pair in python, such as the following.

a = {'44': [0, 0, 1, 0, 1], '43': [0, 0, 1, 0, 0]}

now I want to combine these values with:

b = {'44': ['test1'], '43': ['test2']}

How can I do the output below in python?

c = {'44': [0, 0, 1, 0, 1, 'test1], '43': [0, 0, 1, 0, 0,'test2']}
Carlos Fabiera
  • 79
  • 1
  • 1
  • 10
  • Possible repetition of this https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression – chasmani Oct 27 '17 at 13:18

7 Answers7

1

You can use a dictionary comprehension like this (as of Python 2.7+):

c = {k: a[k] + b[k] for k in a}

This assumes that the same keys exist in both a and b. If that's not the case, it's possible to work around that if necessary.

janos
  • 120,954
  • 29
  • 226
  • 236
1

The other answers are based on the assumption that all dictionaries involved have all the same keys. If you aren't sure that this is the case, I recommend using collections.defaultdict:

from collections import defaultdict

result = defaultdict(list)

for d in (a,b):
    for key, value in d.items():
        result[key].append(value)

Additionally, this solution works for any number of dictionaries. Simply add all dictionaries you want to merge, to the tuple (a,b).

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
0
a = {'44': [0, 0, 1, 0, 1], '43': [0, 0, 1, 0, 0]}
b = {'44': ['test1'], '43': ['test2']}

c = {}

for v in a:
    c[v] = a[v]+b[v]

Output:

{'44': [0, 0, 1, 0, 1, 'test1'], '43': [0, 0, 1, 0, 0, 'test2']}
midor
  • 5,487
  • 2
  • 23
  • 52
voiDnyx
  • 975
  • 1
  • 11
  • 24
0

You can use zip() with dictionary comprehension to achieve that in one-line code like below:

c = {k: v for k, v in zip(a.keys(), (v1 + v2 for v1, v2 in zip(a.values(), b.values())))}

Output:

>>> c
{'44': [0, 0, 1, 0, 1, 'test1'], '43': [0, 0, 1, 0, 0, 'test2']}
ettanany
  • 19,038
  • 9
  • 47
  • 63
0

First thing coming to my mind is :

c = {k: a.get(k, []) + b.get(k, []) for k in {*a.keys(), *b.keys()}}

This should work even if some keys are missing in one dict. I think this only works on the newer versions of python 3 though.

Unatiel
  • 1,060
  • 1
  • 11
  • 17
0

I am assuming here that a and b contain the same keys. Then you can simply use the dict comprehension (Create a dictionary with list comprehension in Python):

c = {x: a[x] + b[x] for x in a}

If you don't know whether b has the same keys, you can also test for that:

c = {x: a[x] + b[x] for x in a if x in b}
Aecturus
  • 163
  • 1
  • 4
0

You can try this:

a = {'44': [0, 0, 1, 0, 1], '43': [0, 0, 1, 0, 0]}
b = {'44': ['test1'], '43': ['test2']}
final_data = {c:a[c]+b[c] for c, d in a.items()}

Output:

{'44': [0, 0, 1, 0, 1, 'test1'], '43': [0, 0, 1, 0, 0, 'test2']}

If you need to generate the "test1", "test2" values, etc, you can try this:

l1 = {}
for c, d in a.items():
   if d not in l1.values(): 
       l1["test{}".format(len(l1)+1)] = d

print(l1)

Output:

{'test1': [0, 0, 1, 0, 1], 'test2': [0, 0, 1, 0, 0]}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102