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}

Tags: Python

About the Author


R Donato

Rick Donato is the Founder and Chief Editor of He currently works as an SDN/NFV Solutions Architect and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001