Python – How do I merge 2 dictionaries ?

Below shows you how to merge 2 dictionaries. It is also worth noting that any items will be overwritten by the 2nd dictionary should they exist in both. This is also shown below via the bananas items.

>>> x = {'apples': 1, 'bananas': 2}
>>> y = {'bananas':3 , 'coconuts': 4}
>>> dict(x.items() + y.items())
{'coconuts': 4, 'apples': 1, 'bananas': 3}
Rick Donato

Want to become a programming expert?

Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial