Looking for a way to use Python merge dictionaries or use Python combine dictionaries? There are many ways to do it. This article will show you three ways: using the union operator, the update() function, and the **
operator. Now scroll down and find the way that works best for you.
How to use Python merge dictionaries?
Merge, combine, join, concat… they all mean combining two things. Below are the simplest ways to combine two dictionaries in Python that we would like to introduce.
Using the Union |
operator
With List and String, we can easily use the +
operator to join two lists or strings together. But we cannot do the same with Dictionary. So, is there a way to combine two dictionaries with just one operator?
The answer is yes. In Python 3.9, we can use Python combine dictionaries just with the Union |
operator.
Syntax:
dict1 | dict2
Description:
This expression will return the Union of dict1 and dict2.
Now, try to merge two dictionaries with just one operator.
bookmark1 = {"learn": "learnshareit.com", "shopping": "amazon.com", "search": "google.com"} bookmark2 = {"shopping": "amazon.com", "search": "google.com", "book": "scribd.com"} # Merge two dictionaries with the Union operator bookmark = bookmark1 | bookmark2 print(bookmark) # Expected output: the merged dictionary
Output:
{
'learn': 'learnshareit.com',
'shopping': 'amazon.com',
'search': 'google.com',
'book': 'scribd.com'
}
Using the update()
function
The update() function adds the Dictionary’s elements to another Dictionary. So you can use it to add two dictionaries Python.
Syntax:
dict1.update(dict2)
Parameters:
- dict1: the Dictionary you want to update.
- dict2: the Dictionary used to add to dict1.
This function is a void function because it updates directly to dict1.
When you use the update()
function to add the dict2’s elements to dict1, Python will add the key-value pairs in dict2 to dict1 and overwrite the duplicate keys with the value of dict2. See the below example for how to use the update()
function.
bookmark = {"learn": "learnshareit.com", "shopping": "amazon.com", "search": "google.com"} bookmarkNew = {"shopping": "amazon.com", "search": "google.com", "book": "scribd.com"} # Combine dictionaries using update() bookmark.update(bookmarkNew) print(bookmark) # Expected output: the combined dictionary
Output:
{
'learn': 'learnshareit.com',
'shopping': 'amazon.com',
'search': 'google.com',
'book': 'scribd.com'
}
Using the double asterisk **
The double asterisk **
is mainly used in the function definition. This special syntax allows us to pass as many arguments to the function as we want.
The **
operator has similar usage to the Rest operator in JavaScript. You will not be confused when using the **
operator if you have used Rest in JavaScript.
To combine dictionaries using the **
operator, we need to put it before dict1
and dict2
and wrap them with curly braces { }
. All elements inside dict1
and dict2
will become elements of the outer curly braces. Like this:
bookmark1 = {"learn": "learnshareit.com", "shopping": "amazon.com", "search": "google.com"} bookmark2 = {"shopping": "amazon.com", "search": "google.com", "book": "scribd.com"} def merge(dict1, dict2): # Combine two dictionaries using the ** operator return {**dict1, **dict2} # The bookmark object is created by the items of bookmark1 and bookmark2 bookmark = merge(bookmark1, bookmark2) print(bookmark) # Expected output: the merged dictionary
Output:
{
'learn': 'learnshareit.com',
'shopping': 'amazon.com',
'search': 'google.com',
'book': 'scribd.com'
}
Summary
This article has answered the question of how to use Python combine dictionaries or add two dictionaries Python. If your Python version is 3.9 or later, use the Union operator to solve the problem. It will make your code shorter and easier to understand.
Visit LearnShareIT daily to keep up to date with the latest Python knowledge!
Maybe you are interested:
- Return a default value if Key is not in Dictionary in Python
- Sum all values in a dictionary in Python
- Mapping over dictionary values in Python

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java