How Do I Merge Two Dictionaries in a Single Expression (Take Union of Dictionaries)?
Naveera A.
—You’d like to merge two or more Python dictionaries in a single expression. For example, you have the following dictionaries storing some data about fictional characters:
c1 = { 'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz' } c2 = { 'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès' }
How can you merge them into a single dictionary in a single line of code?
Depending on which version of Python you’re using, you can choose one of the following methods to merge Python dictionaries.
|
OperatorPython 3.9 introduced the merge operator(|
) in the dict
class. Using the merge operator is the easiest way to merge dictionaries. The merge operator returns a new dictionary, leaving the original dictionaries unchanged.
>>> c = c1 | c2 >>> c {'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz', 'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès'}
We can change the order of the dictionaries, like so:
>>> c = c2 | c1 >>> c {'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès', 'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz'}
We can merge multiple dictionaries at once using this method.
|=
OperatorIf we want to merge the dictionaries in-place, we can use the update operator (|=
):
>>> c1 |= c2 >>> c1 {'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz', 'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès'} >>> c2 {'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès'}
We can merge multiple dictionaries at once using this method.
Like the merge operator, the update operator will work with Python 3.9 and above only.
**
OperatorIn Python 3.5 and above, we can also use the unpacking operator (**
) to merge the dictionaries:
>>> c3 = {**c1, **c2} >>> c3 {'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz', 'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès'}
Using the unpacking operator doesn’t change the original dictionaries. If you want to merge the dictionaries in-place, you cannot use the unpacking operator. We can merge more than two dictionaries at once using this method.
dict.update()
MethodFor Python versions below 3.5, we can use the update()
method of the dict
class to merge one dictionary into another:
>>> c1.update(c2) >>> c1 {'The Hound of the Baskervilles': 'Sherlock Holmes', 'To Kill A Mockingbird': 'Scout Finch', 'Heart of Darkness': 'Kurtz', 'Lord of the Rings': 'Frodo Baggins', 'The Count of Monte Cristo': 'Edmond Dantès'}
Using this method, we can only merge the dictionaries in-place, i.e. one of the dictionaries will be modified. So if we need to keep the original dictionaries unchanged, we need to create a copy of one of the dictionaries and then merge the second one into this copy.
However, we can only merge two dictionaries at a time using this method.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.