Fix Python – Adding dictionaries together [duplicate]

Question

Asked By – rectangletangle

I have two dictionaries and I’d like to be able to make them one:

Something like this pseudo-Python would be nice:

dic0 = {'dic0': 0}
dic1 = {'dic1': 1}

ndic = dic0 + dic1
# ndic would equal {'dic0': 0, 'dic1': 1}

Now we will see solution for issue: Adding dictionaries together [duplicate]


Answer

If you’re interested in creating a new dict without using intermediary storage: (this is faster, and in my opinion, cleaner than using dict.items())

dic2 = dict(dic0, **dic1)

Or if you’re happy to use one of the existing dicts:

dic0.update(dic1)

This question is answered By – bluepnume

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0