-5

I have two dictionaries:

dic_1={'1234567890': 1, '1234567891': 2, '1234567880': 3, '1234567881': 4}
dic_2={'1234567890': 5, '1234567891': 6}

Now I want to merge them based on key values such that the merged dictionary looks like the following:

merged_dic=={'1234567890': 1, '1234567891': 2, '1234567880': 3, '1234567881': 4}

We only want to keep unique keys and only one distinct value associated with them. What's the best way to do that

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    Is there a specific performance issue with your current implementation? – jonrsharpe May 02 '19 at 19:31
  • i dont understand the question, whats the difference between dic_1 and merged_dic ? – Liam F-A May 02 '19 at 19:37
  • 1
    What actually gets merged here? your output is the same as `dic_1` – Sayse May 02 '19 at 19:37
  • so the merged dictionary checks if there is already a value assigned for a key, for example in Dic_2 "123456789" has value 5, but in dic_1 the value for same number is 1, so when we merge the dictionary the merged dictionary will go with whatever value was assigned in dictionary one ofr the number 123456789, – pythonsavvy May 02 '19 at 19:46
  • So you want to use `update()` but keep the old keys and values instead of have it overwrite them? – Error - Syntactical Remorse May 02 '19 at 19:48
  • 1
    Possible duplicate of [How to merge two dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression) – Sayse May 02 '19 at 19:49
  • You're essentially just trying to update `dic_2` with values from `dic_1` – Sayse May 02 '19 at 19:50
  • so dic_1 and dic_2 will have some common keys and some distinct keys, the merged dictionary should look for common keys and if any value has already been assigned with it, it will go with it, and if the key is not common then it will keep adding, to sum up the merged dictionary should only have distinct keys with a value assigned with it – pythonsavvy May 02 '19 at 19:52
  • You should show what you've tried. Its possible to do this in 17 characters. – Sayse May 02 '19 at 19:54

4 Answers4

1

This should be what you need. It iterates through all dictionaries adding key/values only if the key is not already in the merged dictionary.

from itertools import chain

merged_dic = {}

for k, v in chain(dic_1.items(), dic_2.items()):
    if k not in merged_dic:
        merged_dic[k] = v

print(merged_dic)
# {'1234567890': 1, '1234567891': 2, '1234567880': 3, '1234567881': 4}

If, for example, you were wanting to keep all values for a key you could use:

from collections import defaultdict
from itertools import chain

merged_dic = defaultdict(list)

for k, v in chain(dic_1.items(), dic_2.items()):
    merged_dic[k].append(v)

print(merged_dic)
# {'1234567890': [1, 5], '1234567891': [2, 6], '1234567880': [3], '1234567881': [4]}

Using chain() can allow you to iterate over many dictionaries. In the question you showed 2 dictionaries, but if you had 4 you could easily merge them all. E.g.

for k, v in chain(dic_1.items(), dic_2.items(), dic_3.items(), dic_4.items()):
Roqux
  • 608
  • 1
  • 11
  • 25
0

The sample data is not exactly explains the SO. If dic_2 has common key with dic_1 then retain the item in dic_1; if new item is found in dic_2 then put it in merged dictionary.

import copy
dic_1={'1234567890': 1, '1234567891': 2, '1234567880': 3, '1234567881': 4}
dic_2={'1234567890': 5, '8234567890': 6}

merged_d = copy.copy(dic_1)
diff = set(dic_2)-set(dic_1)
merged_d.update({k: dic_2[k] for k in diff})
print(merged_d)

Result:

{'1234567890': 1, '1234567891': 2, '1234567880': 3, '1234567881': 4, '8234567890': 6}
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • 1
    You would want to copy `dic_1` rather than use `merged_d = dic_1` as any updates to `dic_1` will also update `merged_d` (i.e. they point to the same object) :) – Roqux May 02 '19 at 20:13
0

All you're really trying to do is update dic_2 with any values in dic_1 so you can just do

merged_dic = {**dic_2,**dic_1}

This will merge the two dictionaries, taking all the values from dic_2, updating any keys in the new dictionary with any new values that exist in dic_1 and then adding any unique keys in dic_1

Sayse
  • 42,633
  • 14
  • 77
  • 146
0

If you want the first dict to override the keys in the second dict then:

dic_2.update(dic_1)
Steve McCartney
  • 191
  • 1
  • 3