Sentry Answers>Python>

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

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:

Click to Copy
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.

Click to Copy
>>> 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:

Click to Copy
>>> 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 (|=):

Click to Copy
>>> 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:

Click to Copy
>>> 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:

Click to Copy
>>> 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.

  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Syntax.fmListen to the Syntax Podcast
  • Sentry BlogLogging in Python: A Developer’s Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “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.

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