How to Merge Dictionaries in Python
Three strategies to consolidate dictionaries
Merging data—often referred to as a union operation—is a common task in programming. When it comes to merging dictionaries, there are multiple strategies that you can choose from.
In this article, we’ll detail three different strategies, one of which is brand new in version 3.9 and by far the easiest of the three. Each strategy has its pros and cons; by the end of this article you will be able to make an educated decision on which to use for your next project.
The .update() Method
Our first strategy is using a method built into the dictionary class. All dictionaries have a method named update() that will merge a supplied dictionary into the original.d1 = { "name": "Jon", "age": 22 }
d2 = { "email": "jon@email.com" }
d1.update(d2)
print(d1) # { "name": "Jon", "age": 22, "email": "jon@email.com" }
So what’s good and bad about the update() method? The good is that it is both native and semantically makes sense. Unfortunately, the downside to the method is that it modifies the original dictionary and in practice we often want to preserve this, causing the need for a third dictionary.d1 = { "name": "Jon", "age": 22 }
d2 = { "email": "jon@email.com" }d3 = dict()
d3.update(d1)
d3.update(d2)
If you are okay mutating your original data set, then feel free to use the update() method. Otherwise, there are better options available.
The Keyword Splat Operator
Our second strategy is not as readable or what the Python team term “discoverable” as the update() method; however, it is compact and addresses the issue of modifying the original data set.
The keyword splat operator (**) is used to “unpack” dictionaries. This can be a confusing concept, so I like to use the analogy of photocopying sheets of paper in one folder and putting them in another.
Let’s return to our original example and refactor it using the splat operator.d1 = { "name": "Jon", "age": 22 }
d2 = { "email": "jon@email.com" }
d3 = {**d1, **d2}
print(d3) # { "name": "Jon", "age": 22, "email": "jon@email.com" }
If it isn’t readily apparent, what’s happening is that we are unpacking d1 and d2 into a new dictionary and assigning that to d3. The big takeaways here are that our original dictionaries are not modified and only one line is required to create the new dictionary. On the downside, if you’re new to Python then this syntax is not very readable or discoverable.
Fortunately, starting in Python 3.9, we have a solution that is both readable and efficient…
The Union Operator
That Python continues to improve accessibility to its tools is something I absolutely love. We’ve already detailed two strategies to accomplish the same goal; however, the language did not stop there.
Released in 3.9, the union operator is an easy-to-read, concise operator responsible for merging dictionaries both into new and existing structures.
Let’s dive on in!d1 = { "name": "Jon", "age": 22 }
d2 = { "email": "jon@email.com" }
d3 = d1 | d2
print(d3) # { "name": "Jon", "age": 22, "email": "jon@email.com" }
We’ve replaced the keyword splat operator, and creating a new dictionary, with a single vertical pipe. But, what if we wanted to modify our original dictionary? No worries, the union operator can be used with an equal sign as a compound assignment operator.d1 = { "name": "Jon", "age": 22 }
d2 = { "email": "jon@email.com" }
d1 |= d2
print(d1) # { "name": "Jon", "age": 22, "email": "jon@email.com" }
Simple.
Remember that this is not available before 3.9 so make sure your version of Python supports this awesome addition before implementing.
I hope you liked this article! Subscribe to be notified when I publish new articles or even better, consider joining the Medium community as a paid subscriber.