As a developer, there are times where you may end up with two or more dictionaries that you need to combine into one master dictionary. There are lots of different ways to merge dictionaries in the Python programming language.
In this tutorial, you will look at a few of the old ways to merge dictionaries and then look at the latest method that was added in Python 3.9.
Here are the methods you will learn about:
- Using dict.update()
- Merging with **
- Merging with the Union Operator
You will start your journey with the update() method!
Using dict.update()
Python’s dictionary has many different methods. One of those methods can be used to merge two dictionaries together. That method is called update().
Here is an example:
>>> first_dictionary = {"name": "Mike", "occupation": "Python Teacher"} >>> second_dictionary = {"location": "Iowa", "hobby": "photography"} >>> first_dictionary.update(second_dictionary) >>> first_dictionary {'name': 'Mike', 'occupation': 'Python Teacher', 'location': 'Iowa', 'hobby': 'photography'}
This worked perfectly! The only problem with this method is that it modifies one of the dictionaries. If you want to create a third dictionary without modifying one of the input dictionaries, then you’ll want to check out one of the other merging methods in this article.
You are now ready to learn about using **!
Merging with **
When you use the double-asterisk, it is sometimes called “unpacking”, “expanding” or “splatting” a dictionary. The ** is used in Python with kwargs in functions too.
Here is how you can use the ** to merge two dictionaries:
>>> first_dictionary = {"name": "Mike", "occupation": "Python Teacher"} >>> second_dictionary = {"location": "Iowa", "hobby": "photography"} >>> merged_dictionary = {**first_dictionary, **second_dictionary} >>> merged_dictionary {'name': 'Mike', 'occupation': 'Python Teacher', 'location': 'Iowa', 'hobby': 'photography'}
This syntax looks a little weird, but it works great!
Now you are ready to learn about the latest way to merge two dictionaries!
Merging with the Union Operator
Starting in Python 3.9, you can use Python’s union operator, |, to merge dictionaries. You can learn all the nitty-gritty details in PEP 584.
Here is how you can use the union operator to merge two dictionaries:
>>> first_dictionary = {"name": "Mike", "occupation": "Python Teacher"} >>> second_dictionary = {"location": "Iowa", "hobby": "photography"} >>> merged_dictionary = first_dictionary | second_dictionary >>> merged_dictionary {'name': 'Mike', 'occupation': 'Python Teacher', 'location': 'Iowa', 'hobby': 'photography'}
This is the shortest method yet for merging two dictionaries into one.
Wrapping Up
You now know three different methods that you can use to merge multiple dictionaries into one. If you have access to Python 3.9 or greater, you should use the union operator as that is arguable the cleanest looking method of combining dictionaries. However, if you are stuck on an older version of Python, you needn’t despair as you now have two other methods that should work!