- Python: Dictionary Union Operators Explained
- Merging two Dictionaries using dict.update [Old Way]
- Merging two Dictionaries using the Union Operator [Python 3.9+]
- Merging two Dictionaries using the Augmented Assignment Union Operator [Python 3.9+]
- Conclusion
- Learning Python?
- Featured Posts
- Are you Beginning your Programming Career?
- I provide my best content for beginners in the newsletter.
- How to Merge Dictionaries in Python
- How to Merge Dictionaries in Python Using a For Loop
- How to Merge Dictionaries in Python Using the dict.update() Method
- How to Merge Dictionaries in Python Using the ** operator
- How to Merge Dictionaries in Python Using the | Operator
- Conclusion
Python: Dictionary Union Operators Explained
We all have those moments when we want to merge two Python dictionaries together based on their keys. The traditional way to do this was to use the dict.update() method. However starting Python 3.9, the much-awaited dict Union operator (| and |=) was introduced.
So what are these new Union operators and how to use them?
This is what I will discuss in this article.
Merging two Dictionaries using dict.update [Old Way]
Say you want to merge two dictionaries d1 and d2 . Here is how you would do it using the dict.update method
>>> d1 = >>> >>> d1 = >>> d2 = >>> d1.update(d2) >>> d1
A couple of things to notice:
- the update method didn’t create a new dictionary object, it just updated d1 in place
- the keys of the updated d1 is the union of the keys in d1 and d2
- for keys that exist in both d1 and d2 , the values of those keys will be the ones in d2 . In other words, d1 is actually “updated” with d2
Now let’s see how we can do the same thing with the Union operator (|)
Merging two Dictionaries using the Union Operator [Python 3.9+]
Easy and simple, this is how you do it.
As you can see, we get the same result as before. d1 is merged with d2 and keys that exist in both dictionaries are updated with the values of the operand to the right of the union operator.
There is one difference between using the union operator and the update method though.
As you can see from the code above, the union operator actually returns a new dictionary. It doesn’t modify d1 or d2 in place.
So what if you want to update d1 in place instead of creating a new dictionary?
In this case, you can still use the dict.update method, or even better, the augmented assignment union operator (|=)
Merging two Dictionaries using the Augmented Assignment Union Operator [Python 3.9+]
If you want to modify the dictionary d1 in place, here is how to do it using the union operator.
Conclusion
Starting Python 3.9+, you can now use the Union operator to merge two dictionaries together.
To merge two dictionaries in place:
To merge two dictionaries and return the result in a new dictionary object:
Learning Python?
Featured Posts
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- Python tips for beginners, intermediate, and advanced levels.
- CS Career tips and advice.
- Special discounts on my premium courses when they launch.
How to Merge Dictionaries in Python
Ashutosh Krishna
In Python, a dictionary is a collection you use to store data in pairs. It is ordered and mutable, and it cannot store duplicate data.
We write a dictionary using curly brackets like this:
Sometimes, we need to merge two or more dictionaries to create a bigger dictionary. For example:
dict_one = < "id": 1, "name": "Ashutosh", "books": ["Python", "DSA"] >dict_two = < "college": "NSEC", "city": "Kolkata", "country": "India" >merged_dict =
In the merged_dict we have the key-value pairs of both dict_one and dict_two . This is what we wish to achieve programmatically.
There are various ways we can do this in Python:
- Using a for loop
- Using the dict.update() method
- Using the ** operator
- Using the | (Union) operator (for Python 3.9 and above)
Let’s explore each way one after another.
How to Merge Dictionaries in Python Using a For Loop
We can merge two or more dictionaries using for loop like this:
>>> dict_one = < . "id": 1, . "name": "Ashutosh", . >>>> dict_two = < . "books": ["Python", "DSA"], . "college": "NSEC", . >>>> dict_three = < . "city": "Kolkata", . "country": "India" . >>>> for key,value in dict_two.items(): . merged_dictPython union two dictionaries = value . >>> merged_dict >>> for key,value in dict_three.items(): . merged_dictPython union two dictionaries = value . >>> merged_dict
But the problem with this method is that we need to run so many loops to merge the dictionaries.
How to Merge Dictionaries in Python Using the dict.update() Method
If you explore the dict class, there are various methods inside it. One such method is the update() method which you can use to merge one dictionary into another.
>>> dict_one = < . "id": 1, . "name": "Ashutosh", . "books": ["Python", "DSA"] . >>>> dict_two = < . "college": "NSEC", . "city": "Kolkata", . "country": "India" . >>>> dict_one.update(dict_two) >>> dict_one
But the problem when we use the update() method is that it modifies one of the dictionaries. If we wish to create a third dictionary without modifying any of the other dictionaries, we cannot use this method.
Also, you can only use this method to merge two dictionaries at a time. If you wish to merge three dictionaries, you first need to merge the first two, and then merge the third one with the modified dictionary.
>>> dict_one = < . "id": 1, . "name": "Ashutosh", . >>>> dict_two = < . "books": ["Python", "DSA"], . "college": "NSEC", . >>>> dict_three = < . "city": "Kolkata", . "country": "India" . >>>> dict_one.update(dict_two) >>> dict_one >>> dict_one.update(dict_three) >>> dict_one
Let’s explore some other options.
How to Merge Dictionaries in Python Using the ** operator
You can use the double-asterisk (**) method to unpack or expand a dictionary like this:
>>> dict_one = < . "id": 1, . "name": "Ashutosh", . >>>> dict_two = < . "books": ["Python", "DSA"] . "college": "NSEC", . >>>> dict_three = < . "city": "Kolkata", . "country": "India" . >>>> merged_dict = <**dict_one, **dict_two, **dict_three>>>> merged_dict
Using the ** operator to merge the dictionaries doesn’t affect any of the dictionaries.
How to Merge Dictionaries in Python Using the | Operator
Starting with Python 3.9, we can use the Union ( | ) operator to merge two or more dictionaries.
>>> dict_one = < . "id": 1, . "name": "Ashutosh", . >>>> dict_two = < . "books": ["Python", "DSA"], . "college": "NSEC", . >>>> dict_three = < . "city": "Kolkata", . "country": "India" . >>>> merged_dict = dict_one | dict_two | dict_three >>> merged_dict
This is the most convenient method available for merging dictionaries in Python.
Conclusion
We have explored several different methods for merging dictionaries. If you have Python 3.9 or above, you should use the | operator. But if you use older versions of Python, you can still use the other methods discussed above.
Ashutosh Krishna
Application Developer at Thoughtworks India
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.