7 Different Ways to Merge Dictionaries in Python
Different ways of merging dictionaries in Python
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
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}