Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging of two dictionaries [duplicate]

Tags:

python

I have two dictionaries as follows:

D1={'a':1,'b':2,'c':3}

and

D2={'b':2,'c':3,'d':1}

I want to merge these two dictionaries and the result should be as follows:

D3={'a':1,'b':2,'c':3,'b':2,'c':3,'d':1}

how can I achieve this in python?

like image 399
Pravitha V Avatar asked Sep 20 '12 11:09

Pravitha V


People also ask

Can we merge two dictionaries?

In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

Do dictionaries allow duplicates?

The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.

How do you combine dictionaries in Python?

Python 3.9 has introduced the merge operator (|) in the dict class. Using the merge operator, we can combine dictionaries in a single line of code. We can also merge the dictionaries in-place by using the update operator (|=).


2 Answers

I want to merge these two dictionaries and the result should be as follows:

D3={'a':1,'b':2,'c':3,'b':2,'c':3,'d':1}

how can I achieve this in python?

You can't. You can only have one value per key in a Python dict. What you can do is to have a list or a set as the value.

Here is an example with a set:

d1 = { 'a': 1, 'b': 2, 'c': 3 }
d2 = { 'a': 1, 'b': 5, 'd': 4 }
d3 = {}
def add_dict(target, d):
    for key in d:
        target.setdefault(key, set([])).add(d[key])

add_dict(d3, d1)
add_dict(d3, d2)

This will give you d3:

{'a': set([1]), 'c': set([3]), 'b': set([2, 5]), 'd': set([4])}

You can also do this with a list (possibly closer to your example):

d1 = { 'a':1, 'b':2, 'c': 3}
d2 = { 'b':2 ,'c':3, 'd': 1}
d3 = {}
def add_dict(target, d):
    for key in d:
        target.setdefault(key, []).append(d[key])

add_dict(d3, d1)
add_dict(d3, d2)

You'll get this:

{'a': [1], 'c': [3, 3], 'b': [2, 2], 'd': [1]}

However, looking at {'a':1,'b':2,'c':3,'b':2,'c':3,'d':1} (which can't be a dict), it seems that you're after a different data structure altogether. Perhaps something like this:

d1 = { 'a':1, 'b':2, 'c': 3}
d2 = { 'b':2 ,'c':3, 'd': 1}
result = []

result += [ { 'key': key, 'value': d1[key] } for key in d1 ]
result += [ { 'key': key, 'value': d2[key] } for key in d2 ]

This would produce this, which looks closer to the data structure you had in mind initially:

[ {'value': 1, 'key': 'a'},
  {'value': 3, 'key': 'c'},
  {'value': 2, 'key': 'b'},
  {'value': 3, 'key': 'c'},
  {'value': 2, 'key': 'b'},
  {'value': 1, 'key': 'd'} ]
like image 187
Bruno Avatar answered Sep 19 '22 05:09

Bruno


You may want to try:

D3 = {}
D3.update(D1)
D3.update(D2)

Here, we're creating an empty dictionary D3 first, then update it from the two other dictionaries.

Note that you need to be careful with the order of the updates: if D2 shares some keys with D1, the code above will overwrite the corresponding entries of D1 with those of D2.

Note as well that the keys will not be repeated. The example you give D3={a:1,b:2,c:3,b:2,c:3,d:1} is not a valid dictionary.

like image 29
Pierre GM Avatar answered Sep 18 '22 05:09

Pierre GM