7 Different Ways to Merge Dictionaries in Python

Different ways of merging dictionaries in Python

Indhumathy Chelliah
Level Up Coding

Merging Python dictionaries

In python, we can merge two dictionaries using different methods. Let’s learn about this in this article.

Refer to my article for Python dictionaries.

Different ways of merging two dictionaries

Image Source: Author

1. dict.update()

update([other])

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None. — python docs

Example 1:Merging two dictionaries d1,d2 having unique keys using the update() method.

d1.update(d2)

Update d1- dictionary with key-value pairs from d1 and d2.

The return type is None. It will update the original dictionary d1.

d1={'a':1,'b':2}
d2={'c':3,'d':4}
d1.update(d2)
print (d1)
#Output:{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

What are your thoughts?