3 Simple Ways to Merge Python Dictionaries

Learn how to efficiently merge Python dictionaries using built-in methods, unpacking, and the union operator.



3 Simple Ways to Merge Python Dictionaries
Image by Author | Created on Canva

 

When working with dictionaries in Python, you’ll sometimes have to merge them into a single dictionary for further processing.

In this tutorial, we'll go over three common methods to merge Python dictionaries. Specifically, we’ll focus on merging dictionaries using:

  • The update() method
  • Dictionary unpacking
  • The union operator

Let’s get started.

Note: You can find the code examples on GitHub.

 

1. Using the update() Method

 

To merge two dictionaries, you can use the update() dictionary method. You can use the update() method to update an existing dictionary with key-value pairs from another dictionary like so:

dict1.update(dict2)

 

Here:

  • dict1: The original dictionary to be updated.
  • dict2: The dictionary whose key-value pairs will be added to dict1.

 

Note: This method directly modifies the original dictionary. If you don’t want that, you can create a copy of the original dictionary and update the copy.

 

Let’s say we have two configuration dictionaries, config1 and config2 containing config info to connect to a database:

config1 = {
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432
}

config2 = {
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

 

Now let’s merge a copy of config1 with config2:

# Merge using update() method
final_config = config1.copy()  # Create a copy to avoid modifying the original config1
final_config.update(config2)

print(final_config)

 

As seen, the update() method adds all key-value pairs from config2 to final_config, resulting in a combined dictionary with all the config info.

Output >>>
{
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432,
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

 

2. Using Dictionary Unpacking: {**dict1, **dict2}

 

Just the way you unpack tuples, you can unpack dictionaries as well using **. Doing so creates a new dictionary by unpacking the key-value pairs from both dictionaries.

Say you have dictionaries dict1 and dict2. You can unpack the key-value pairs and merge them into a new dictionary like so:

merged_dict = {**dict1, **dict2}

 

Let’s merge the config dictionaries as shown:

config1 = {
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432
}

config2 = {
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

# Merge using dictionary unpacking
final_config = {**config1, **config2}

print(final_config)

 

The dictionary unpacking method {**config1, **config2} creates a new dictionary final_config that contains all the key-value pairs from both config1 and config2.

Output >>>
{
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432,
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

 

3. Using the Union | Operator

 

The union operator, introduced in Python 3.9, is another concise way to merge two dictionaries.

You can use the union operator like so:

merged_dict = dict1 | dict2

 

Using dict1 | dict2 merges dict1 and dict2 into a new dictionary.

config1 = {
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432
}

config2 = {
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

# Merge using | operator
final_config = config1 | config2

print(final_config)

 

The | operator merges config1 and config2 into a new dictionary final_config:

Output >>>
{
	'database': 'Postgres',
	'host': 'localhost',
	'port': 5432,
	'username': 'admin',
	'password': 'secret',
	'timeout': 30
}

 

Wrapping Up

 

Let’s review the different methods to merge Python dictionaries:

  • The update() method—dict1.update(dict2)—updates an existing dictionary.
  • {**dict1, **dict2} creates a new merged dictionary using unpacking.
  • The union | operator in Python 3.9+ merge dictionaries with the simple syntax dict1 | dict2.

This is not an exhaustive list covering all the methods to merge dictionaries. There are other methods such as list comprehensions, creating a merged view of dictionaries using collections.ChainMap and more. But these are the most common methods to merge dictionaries, though.

Happy coding!
 
 

Bala Priya C is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she's working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.


Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy


Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy

Get the FREE ebook 'KDnuggets Artificial Intelligence Pocket Dictionary' along with the leading newsletter on Data Science, Machine Learning, AI & Analytics straight to your inbox.

By subscribing you accept KDnuggets Privacy Policy

No, thanks!