How Do I Merge Two Dictionaries in a Single Expression (Take Union of Dictionaries)?

Naveera A.

The Problem

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?

The Solution

Depending on which version of Python you’re using, you can choose one of the following methods to merge Python dictionaries.

Merging Dictionaries Using the | Operator

Python 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.

Merging Dictionaries Using the |= Operator

If 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.

Merging Dictionaries Using the ** Operator

In 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.

Merging Dictionaries Using the dict.update() Method

For 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.

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.