← Go Back
Method 2 (creates a new dictionary, Python 3.9 and above):
Method 3 (creates a new dictionary, all versions):
How to Merge Two Dictionaries
Method 1 (updatesd1
in-place):>>> d1 = {"Python": 1991, "C": 1972}
>>> d2 = {"Java": 1996, "C++": 1983}
>>> d1.update(d2)
>>> d1
{'Python': 1991, 'C': 1972, 'Java': 1996, 'C++': 1983}
Method 2 (creates a new dictionary, Python 3.9 and above):
>>> d1 = {"Python": 1991, "C": 1972}
>>> d2 = {"Java": 1996, "C++": 1983}
>>> d3 = d1 | d2
>>> d3
{'Python': 1991, 'C': 1972, 'Java': 1996, 'C++': 1983}
Method 3 (creates a new dictionary, all versions):
>>> d1 = {"Python": 1991, "C": 1972}
>>> d2 = {"Java": 1996, "C++": 1983}
>>> d3 = {**d1, **d2}
>>> d3
{'Python': 1991, 'C': 1972, 'Java': 1996, 'C++': 1983}
🐍 You might also find interesting: