-1

How can I merge three different set of dictionary to new one suppose if input is

d1 = {'name':'tom', 'age':'14', 'sex':'m'}
d2 = {'color':'w', 'weight':'58','style':'good'}
d3 = {'sports':'cricket','music':'rock','dance':'disco'}

Output should be d = {'name':'tom', 'age':'14', 'sex':'m','color':'w', 'weight':'58','style':'good','sports':'cricket','music':'rock','dance':'disco'} I tried using update method, it is suitable for only two dictionary, if I use 3 set it results in repetation,so how can I merge three dictionary into single one

nihkil
  • 111
  • 1
  • 1
  • 7
  • @venky__ not merging , i want to add – nihkil Jan 26 '18 at 16:40
  • how is it different? can you clarify your question? – Equinox Jan 26 '18 at 16:43
  • 1
    Are the keys in the three dictionaries unique? – wwii Jan 26 '18 at 16:59
  • yeah @wwii some are unique, maybe this is why I am getting repetition of one set, How can I fix this? – nihkil Jan 26 '18 at 17:13
  • I see four answers that solve your problem. Maybe your use of `repetition` is confusing us: a dictionary cannot have *repetition* in its keys. If the answers given don't solve your problem, you aren't explaining it well enough - you should show your attempt, its result and show/explain how that result is wrong ... [mcve]. Welcome to SO, please take the time to read [ask] and the other links on that page. – wwii Jan 26 '18 at 17:18
  • @venky__ these are my actual dictionaries d1 = {'runs1': '11', 'runs2': '2', 'balls1': '32', 'six2': '0', 'fours2': '0', 'name2': 'Amla', 'name1': 'Elgar*', 'balls2': '12', 'six1': '0', 'fours1': '1'} d2 = {'overs5': '0.3', 'name5': 'Bumrah*', 'name6': 'Shami', 'maidens6': '1', 'maidens5': '0', 'runs6': '7', 'overs6': '4', 'wickets6': '1', 'runs5': '2', 'wickets5': '0'} – nihkil Jan 26 '18 at 17:20
  • d3 = {'batting_score_desription': '1st Inns', 'progress2': 'stump', 'bowling_score_wickets': '10', 'teams2': 'RSA vs IND', 'Bowling team': 'IND', 'Batting team': 'RSA', 'bowling_score_runs': '187', 'series2': 'India tour of South Africa, 2017-18', 'batting_score_runs': '194', 'bowling_score_overs': '76.4', 'matchtype2': 'TEST', 'matchid2': '4', 'batting_score_wickets': '10', 'bowling_score_desription': '1st Inns', 'batting_score_overs': '65.5', 'status2': 'RSA need 224 runs', 'matchno2': '3rd Test'} @wwii – nihkil Jan 26 '18 at 17:20
  • @wwii these are my actual dictionaries I iterate these over a loop – nihkil Jan 26 '18 at 17:21

5 Answers5

6

If you're using a recent version of Python (>= 3.5), you can take advantage of unpacking in mapping literals

d1 = {'name':'tom', 'age':'14', 'sex':'m'}
d2 = {'color':'w', 'weight':'58','style':'good'}
d3 = {'sports':'cricket','music':'rock','dance':'disco'}

new_dict = {**d1, **d2, **d3}
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
3

update() will work:

d1 = {'name':'tom', 'age':'14', 'sex':'m'}
d2 = {'color':'w', 'weight':'58','style':'good'}
d3 = {'sports':'cricket','music':'rock','dance':'disco'}

d1.update(d2)
d1.update(d3)
print(d1)
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
1

The Pythonic way:

{**d1, **d2, **d3}

Watch out for duplicate keys in the dictionaries.

tHappy
  • 91
  • 8
0

Is there a problem using two lines? If not, I would recommend:

d1.update(d2)
d1.update(d3)
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0

Yet an other option is to use collections.ChainMap:

>>> from collections import ChainMap
>>> d1 = {'name':'tom', 'age':'14', 'sex':'m'}
>>> d2 = {'color':'w', 'weight':'58','style':'good'}
>>> d3 = {'sports':'cricket','music':'rock','dance':'disco'}
>>> result = ChainMap(d1, d2, d3)
>>> result
ChainMap({'age': '14', 'name': 'tom', 'sex': 'm'}, {'color': 'w', 'style': 'good', 'weight': '58'}, {'music': 'rock', 'dance': 'disco', 'sports': 'cricket'})

The ChainMap will behave mostly like the merged dict you want. And you can always convert it to a plain dict by:

>>> dict(result)
{'name': 'tom', 'dance': 'disco', 'color': 'w', 'weight': '58', 'style': 'good', 'age': '14', 'music': 'rock', 'sex': 'm', 'sports': 'cricket'}

You could also write a simple wrapper function for simplicity of usage:

from collections import ChainMap

def merge_dicts(*dicts):
    return dict(ChainMap(*dicts))

Note: It was introduced in python 3.3

Bakuriu
  • 98,325
  • 22
  • 197
  • 231