← Back to Blog
Friday, April 28 2023

How to Merge Dictionaries in Python – Definitive Guide – 8 methods and code including clean single expression one line options

Discover how to merge dictionaries in Python with this comprehensive guide. Explore standard and advanced techniques, including the update() method, dictionary unpacking, Python 3.9’s union operators, ChainMap, and more. Learn to handle duplicate keys, nested pairs, and merge multiple dictionaries with ease.

Python dictionaries (or dict) are key-value pair data containers that offer versatility and easy access to information.

Given their fundamental nature, it is essential to know how to merge multiple dictionaries into one. In this guide, we will explore ins and out of merging dicts from how to merge two dictionaries to handling multiple ones, from tackling duplicate keys to navigating nested pairs, and more. Plus, we’ll introduce Python 3.9’s latest features!

Understanding Key-Value Pairs in Python Dictionaries

Let’s first demystify Python dictionaries! At their core, dictionaries are collections of key-value pairs. Each unique key is linked to a specific value, and these pairs can be of any data type—strings, numbers, lists, or even other dictionaries.

For example, consider below example of the following dictionary that stores information about a student:

student = {
    'name': 'Alice',
    'age': 20,
    'courses': ['Math', 'Physics'],
    'grades': {'Math': 90, 'Physics': 85}
}
JavaScript

Here, keys like ‘name’ and ‘age’ are paired with values ‘Alice’ and 20. Each such pair is called a key value pair.

With this foundation, we’re set to explore merging dictionaries, starting with the popular update() method. Let’s dive in!

How to Merge Two Dictionaries in Python: An Overview

Merging two dictionaries involves combining the key-value pairs from two dictionaries or more into a one. While in most cases this can be a simple operation, there are specific use cases, where one may need to customize the default behavior like handling duplicate keys.

There are various other methods to perform these merging operations and we’ll delve deeper into each method, providing detailed explanations, code examples, and visualizations.

Standard Methods for Merging Dictionaries

These are some of the standard methods that are easy to use and can help with most of the simple use cases to combine multiple dictionaries.

  1. The update() Method: The update() method is a built-in method of dict class that allows you to update one dictionary with the key-value pairs from another dictionary. If keys in the first dictionary already exist in the second dictionary, their values are updated with the values from the second dictionary.
  2. Dictionary Unpacking: Dictionary unpacking is a concise and convenient method to merge dictionaries in Python 3.5 and later. It allows you to unpack the contents of one dictionary into another using the {**dict1, **dict2} syntax.
  3. The dict() Constructor: The dict() constructor can be used to merge dictionaries by passing the dictionaries to be merged as arguments to the constructor. The constructor creates a new dictionary containing the merged keys and values.

New Unpacking Operator for Merging Dictionaries in Python 3.9

  1. The Unpacking Operator (|): The unpacking operator | allows you to merge two dictionaries in a single expression in one line making the code more concise readable. For example, merged_dict = dict1 | dict2.
  2. The In-Place Unpacking Operator (|=): The in-place unpacking operator |= is a single expression way to merge the second dictionary into the first dictionary, in-place, overwriting the first one. The syntax for this operator is very concise and fits For example, dict1 |= dict2.

Advanced Techniques for Merging Dictionaries

  1. Dictionary comprehension
  2. Using Collections.ChainMap to Merge Dictionaries
  3. Using itertools.chain() to Merge Multiple Dictionaries in Python

Handling Complex Data Structures when Merging Dictionaries

  1. How to Merge Dictionaries with Nested Key-Value Pairs in Python
  2. How to Merge Dictionaries with Complex Data Structures: Tips and Tricks

These methods offer a convenient and efficient way to merge two dictionaries in Python. In the next sections, we’ll explore each method in detail, discuss their advantages and limitations, and provide practical examples to demonstrate their usage.

Using the update() Method to Merge Dictionaries Python

The update() method is allows you to update one dictionary with the key-value pairs from another. If keys in the first dictionary already exist in the second dictionary, their values are updated with the values from the second dictionary. If keys from the second or third dictionary do not exist in the first dictionary, they are added to the first dictionary along with their corresponding values.

How to Use update() to Merge Dictionaries with Both Common and Unique Keys

Let’s start by exploring in the below example, how the update() method behaves when merging two dictionaries with some same keys and some unique keys.

# Define two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Merge dictionaries using the update() method
dict1.update(dict2)

# Output the merged dictionary
print(dict1)
JavaScript

Output:

{'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

As we can see, the value of the common key ‘b’ is updated with the value from dict2, and the unique key ‘d’ from dict2 is added to dict1.

In-Place Merging with update()

Keep in mind, update() modifies the original dictionary. To preserve it, create a copy first:

# Create a copy of dict1 to avoid modifying the original dictionary
merged_dict = dict1.copy()
merged_dict.update(dict2)
JavaScript

How to Merge Multiple Dictionaries with update()

The update() method can also be used to merge more than two dictionaries in a sequential manner. Each subsequent dictionary’s key-value pairs are added to the merged dictionary, and existing keys are updated with new values.

# Define three dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict3 = {'c': 5, 'd': 6}

# Merge dictionaries using the update() method
merged_dict = dict1.copy()
merged_dict.update(dict2)
merged_dict.update(dict3)

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': 3, 'c': 5, 'd': 6}
JavaScript

update() is a very convenient method to merge dict

The Unpacking Approach: A Simple Way to Merge Dictionaries

Meet dictionary unpacking—a sleek way to merge dictionaries in Python 3.5+! With a simple syntax and single expression, you can combine multiple dictionaries in one line of code. Let’s see how it works.

Merging Two Dictionaries Using Unpacking

Use the {**dict1, **dict2} syntax that creates a new dictionary that containing the key-value pairs from both dict1 and dict2. If there are common keys, the values from dict2 will overwrite the values from dict1.

# Two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Merge with unpacking
merged_dict = {**dict1, **dict2}
print(merged_dict)  # Output: {'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

Output:

{'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

The value of the common key ‘b’ is updated with the value from dict2, and the unique key ‘d’ from dict2 is added to the merged dict.

Merging Multiple Dictionaries with Unpacking

We can extend the same syntax to merge multiple dicts with the unpacking approach. Each subsequent dictionary’s key-value pairs are added to the merged dictionary, and existing keys are updated with newest values.

# Define three dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict3 = {'c': 5, 'd': 6}

# Merge dictionaries using dictionary unpacking
merged_dict = {**dict1, **dict2, **dict3}

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': 3, 'c': 5, 'd': 6}
JavaScript

That’s it! Dictionary unpacking is a breeze. Next, let’s explore the dict() constructor for merging dictionaries.

Merging Dictionaries with the dict() Constructor: A Comprehensive Step by Step Guide

Let’s merge dictionaries with the dict() constructor! This handy Python tool creates dictionaries and merges them with ease. Similar to the update method,

  • Common keys between the two dicts get updated with the newest value
  • Unique keys simply get added to the dict

Let’s see how it’s done.

Merge Two Dictionaries Using the dict() Constructor

Use dict(**dict1, **dict2) to merge two dictionaries using the dict() constructor.

# Two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Merge with dict() constructor
merged_dict = dict(**dict1, **dict2)

print(merged_dict)  # Output: {'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

Merging Multiple Dictionaries with the dict() Constructor

Two or more dictionaries can be merged easily by just extending the syntax. Same rules apply – i.e. common keys get updated by newest value and unique keys get added as-is.

dict3 = {'c': 5, 'd': 6}
merged_dict = dict(**dict1, **dict2, **dict3)  
# Output: {'a': 1, 'b': 3, 'c': 5, 'd': 6}
JavaScript

That’s it! The dict() constructor makes merging two or more dictionaries super easy with a compact syntax. Next, let’s explore Python 3.9’s new operators for merging dictionaries.

Python 3.9: Introducing the Unpacking Operator for Merging Dictionaries

Welcome to Python 3.9’s dictionary merging magic—the union operator `|` This operator merges two dictionaries into a single sequence, new one, with simplicity and readability. Common keys? The second dictionary’s values take precedence.

# Two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Merge with union operator
merged_dict = dict1 | dict2
print(merged_dict)  # Output: {'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

The union operator | provides a clean and intuitive way to merge dictionaries in Python 3.9 and later versions.

Python 3.9: In-Place Merging: Using the “|=” Operator to Merge Two Dictionaries

Python 3.9 also introduced the in-place union operator |= which allows you to merge the second dictionary into the first dict, in-place, meaning that the first dict is overwritten.

# Define two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Merge dictionaries in-place using the "|=" operator
dict1 |= dict2

# Output the updated dictionary
print(dict1) # Output: {'a': 1, 'b': 4, 'c': 3, 'd': 5}
JavaScript

The in-place union operator |= is a powerful tool for updating dicts based on the contents of other dicts. Just be careful as it overwrites the first dict!

Advanced Techniques for Merging Dictionaries

When merging dictionaries, we may encounter situations where the same key exists in multiple dictionaries, but with different values. For example, we might want to merge dictionaries with nested key-value pairs, handle duplicate keys in a custom way, or merge multiple dictionaries with complex data structures. In this section, we’ll explore advanced techniques for merging dictionaries in Python and provide practical examples to illustrate their usage.

Dictionary Comprehension & Custom Handling of Common Keys

To achieve custom handling of key value pairs and achieve a desired calculation or concatenation, for example, we can use dictionary comprehension to iterate over the keys and apply the desired operation to the values. Let’s look at an example:

# Define two dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'c': 5, 'd': 6}

# Merge dictionaries and calculate the sum of values for common keys
merged_dict = {key: dict1.get(key, 0) + dict2.get(key, 0) for key in set(dict1) | set(dict2)}

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': 6, 'c': 8, 'd': 6}
JavaScript

As we can see, the values for the common keys ‘b’ and ‘c’ are summed, resulting in values 6 and 8, respectively.

Merge Two or More Dictionaries and Aggregate Values

The same approach can be extended to handle multiple dictionaries with common keys. We can use a loop to iterate over each dictionary and update the values in the merged dictionary accordingly.

# Define three dictionaries with common and unique keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'c': 5, 'd': 6}
dict3 = {'c': 7, 'd': 8, 'e': 9}

# Initialize an empty dictionary for the result
merged_dict = {}

# Merge dictionaries and calculate the sum of values for common keys
for d in [dict1, dict2, dict3]:
    for key, value in d.items():
        merged_dict[key] = merged_dict.get(key, 0) + value

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': 6, 'c': 15, 'd': 14, 'e': 9}
JavaScript

By calculating the sum of values for common keys, we can aggregate data, combine settings, or perform other operations that require merging dictionaries with common keys.

Using Collections.ChainMap to Merge Dictionaries

The collections module in Python provides a powerful data structure called ChainMap that allows us to group multiple dictionaries into a single mapping. Unlike the traditional methods of merging dictionaries, ChainMap does not create a new dictionary but instead creates a view that links the original dictionaries together. In this section, we’ll explore how to use ChainMap to merge dictionaries, discuss its advantages, and provide practical examples to illustrate its usage.

Creating a ChainMap

To create a ChainMap, we need to import the collections module and use the ChainMap class constructor. We can pass any number of dictionaries as arguments to the constructor, and they will be combined into a chainmap class single mapping.

import collections

# Define two dictionaries
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'd': 5}

# Create a ChainMap
chain = collections.ChainMap(dict1, dict2)

# Output the ChainMap
print(chain)
JavaScript

Output:

ChainMap({'a': 1, 'b': 2, 'c': 3}, {'b': 4, 'd': 5})
JavaScript

Accessing Key-Value Pairs in a ChainMap

We can access key-value pairs in a ChainMap just like we would in a regular dictionary. If a key is present in multiple dictionaries, the value from the first dictionary in the chain will be returned.

# Access a key present in the first dictionary
print(chain['a'])  # Output: 1

# Access a key present in the second dictionary
print(chain['d'])  # Output: 5

# Access a key present in both dictionaries
print(chain['b'])  

# Output: 2 (Value from the first dictionary is returned)
JavaScript

Advantages of Using ChainMap

  1. Efficiency: ChainMap is more memory-efficient than creating a new merged dictionary because it creates a view over the original dictionaries without copying their contents.
  2. Dynamic Updates: Changes made to the original dictionaries are automatically reflected in the ChainMap.
  3. Preservation of Original Dictionaries: ChainMap allows us to work with multiple dictionaries without modifying their contents.
  4. Flexibility: We can easily add or remove dictionaries from the chain, reorder them, or create sub-chains.

Merging Multiple Dictionaries with ChainMap

ChainMap can also be used to merge more than two dictionaries. Each subsequent dictionary is added to the chain, and the values from the earlier dictionaries take precedence.

# Define three dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict3 = {'c': 5, 'd': 6}

# Merge dictionaries using ChainMap
chain = collections.ChainMap(dict1, dict2, dict3)

# Output the ChainMap
print(chain)
JavaScript

Output:

ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, {'c': 5, 'd': 6})
JavaScript

ChainMap provides a powerful and flexible way to merge dictionaries in Python, especially when working with large or dynamic data sets.

Using itertools.chain() to Merge Multiple Dictionaries in Python

The itertools module in Python provides a powerful function called chain() that allows us to combine multiple iterables into a single iterable. While chain() is not specifically designed for merging dictionaries, it can be used in conjunction with the dict() constructor to achieve this goal. In this section, we’ll explore how to use itertools.chain() to merge multiple dictionaries and provide practical examples to illustrate its usage.

Merging Multiple Dictionaries with itertools.chain()

To merge multiple dictionaries using itertools.chain(), we need to import the itertools module and use the chain() function to concatenate the items from each dictionary. We can then pass the result of chain object to the dict() constructor to create a new dictionary containing the merged pairs of keys and values.

import itertools

# Define three dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict3 = {'c': 5, 'd': 6}

# Merge dictionaries using itertools.chain()
merged_dict = dict(itertools.chain(dict1.items(), dict2.items(), dict3.items()))

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': 3, 'c': 5, 'd': 6}
JavaScript

As we can see above example, the values for the common keys ‘b’ and ‘c’ are updated with the values from the later dictionaries.

Advantages of Using itertools.chain()

  1. Simplicity: itertools.chain() provides a simple and readable way to merge multiple dictionaries.
  2. Flexibility: We can merge any number of dictionaries by passing them as arguments to itertools.chain().
  3. Efficiency: itertools.chain() creates an iterator, making it memory-efficient when working with large dictionaries.

itertools.chain() provides an alternative way to merge dictionaries in Python, especially when we need to merge multiple dictionaries in a single expression.

How to Merge Dictionaries with Nested Key-Value Pairs in Python

To merge nested dictionaries, we can use a recursive function that merges the inner dictionaries at each level.

def merge_nested_dicts(d1, d2):
    for key, value in d2.items():
        if key in d1 and isinstance(d1[key], dict) and isinstance(value, dict):
            merge_nested_dicts(d1[key], value)
        else:
            d1[key] = value

# Define two dictionaries with nested key-value pairs
dict1 = {'a': 1, 'b': {'x': 2, 'y': 3}}
dict2 = {'b': {'y': 4, 'z': 5}, 'c': 6}

# Merge nested dictionaries
merge_nested_dicts(dict1, dict2)

# Output the merged dictionary
print(dict1)
JavaScript

Output:

{'a': 1, 'b': {'x': 2, 'y': 4, 'z': 5}, 'c': 6}
JavaScript

The merge_nested_dicts function recursively merges the inner dictionaries, updating the values for common keys and adding new key-value pairs.

With these techniques, you’re equipped to tackle complex dictionary merging scenarios in Python!

Merging Dictionaries with Complex Data Structures: Tips and Tricks

Merging dictionaries with complex data structures, such as lists or custom objects, requires careful consideration. Here are some tips and tricks:

  • Use the copy module to create deep copies of mutable objects to avoid modifying the original data.
  • Define custom merge functions to handle specific data types or structures.
  • Consider using third-party libraries, such as jsonmerge, for merging complex JSON-like dictionaries.
import copy

def merge_lists(list1, list2):
    return list1 + list2

# Define two dictionaries with complex data structures
dict1 = {'a': 1, 'b': [1, 2], 'c': {'x': 2}}
dict2 = {'b': [3, 4], 'c': {'y': 3}, 'd': 4}

# Merge dictionaries with complex data structures
merged_dict = {}
for key in set(dict1) | set(dict2):
    if key in dict1 and key in dict2:
        if isinstance(dict1[key], list) and isinstance(dict2[key], list):
            merged_dict[key] = merge_lists(dict1[key], dict2[key])
        elif isinstance(dict1[key], dict) and isinstance(dict2[key], dict):
            merged_dict[key] = merge_nested_dicts(copy.deepcopy(dict1[key]), dict2[key])
        else:
            merged_dict[key] = dict2[key]
    elif key in dict1:
        merged_dict[key] = copy.deepcopy(dict1[key])
    else:
        merged_dict[key] = copy.deepcopy(dict2[key])

# Output the merged dictionary
print(merged_dict)
JavaScript

Output:

{'a': 1, 'b': [1, 2, 3, 4], 'c': {'x': 2, 'y': 3}, 'd': 4}
JavaScript

Practical Use Cases: When is There a Need to Merge Dictionaries?

Dictionaries are a powerful data structure in Python, allowing us to store and organize data as key-value pairs. Merging dictionaries is a common operation that involves combining key-value pairs from two or more dictionaries into a single dictionary. There are several scenarios where merging dictionaries is necessary or beneficial. In this section, we’ll explore some of the common use cases where merging dictionaries is needed and discuss how it can enhance data processing and manipulation.

Combining Configuration Settings

In software development, configuration settings are often stored in dictionaries. When building a complex application, there may be default settings and user-specific settings. Merging dictionaries allows us to combine these settings, giving priority to user-specific settings while retaining default values for unspecified options.

default_settings = {'theme': 'light', 'font_size': 12, 'language': 'en'}
user_settings = {'font_size': 14, 'language': 'fr'}

# Merge default and user-specific settings
combined_settings = {**default_settings, **user_settings}
print(combined_settings)

#Output: {'theme': 'light', 'font_size': 14, 'language': 'fr'}
JavaScript

Aggregating Data from Multiple Sources

When working with data from multiple sources, we may need to aggregate information into a single dictionary. Merging dictionaries allows us to consolidate data, update existing entries, and add new information.

data_source1 = {'product1': 100, 'product2': 150}
data_source2 = {'product2': 200, 'product3': 300}

# Merge data from multiple sources
aggregated_data = {**data_source1, **data_source2}
print(aggregated_data)

# Output: {'product1': 100, 'product2': 200, 'product3': 300}
JavaScript

Updating and Extending Dictionaries

Merging dictionaries is useful for updating the values of existing keys and extending dictionaries with new key-value pairs. This is especially helpful when working with dynamic data that changes over time.

original_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
update_dict = {'age': 26, 'country': 'USA'}

# Update and extend the original dictionary
original_dict.update(update_dict)
print(original_dict)

# Output: 
# {'name': 'Alice', 'age': 26, 'city': 'New York', 'country': 'USA'}
JavaScript

Merging dictionaries is a versatile operation that enhances our ability to work with key-value pairs in Python. Whether we’re combining settings, aggregating data, or updating information, merging dictionaries provides a flexible and efficient way to manage and manipulate data.

Common Mistakes to Avoid When Merging Dictionaries in Python

When merging dictionaries, it’s important to avoid common mistakes:

  • Modifying the original dictionaries: Use the copy() method or the copy module to create copies of dictionaries before merging.
  • Overwriting values unintentionally: Be mindful of the order in which dictionaries are merged, as values from later dictionaries may overwrite earlier ones.
  • Not handling nested dictionaries: Use recursive functions to handle nested dictionaries and merge them correctly.

Performance Comparison: Which Method to Use to Merge Dictionaries?

The choice of method for merging dictionaries depends on the specific use case and the version of Python being used:

  • The update() method is simple and works in all versions of Python, but it modifies the original dictionary.
  • Dictionary unpacking ({**dict1, **dict2}) is concise and creates a new dictionary, but it’s only available in Python 3.5+.
  • The union operators (| and |=) are efficient and readable, but they are only available in Python 3.9+.
  • ChainMap is memory-efficient and dynamic, but it creates a view rather than a new dictionary.
  • itertools.chain() is flexible and efficient for merging multiple dictionaries.

Consider the size of the dictionaries, the number of dictionaries to merge, and the desired behavior when choosing a method.

Best Practices for Merging Dictionaries with Large Data Sets

  • Use memory-efficient methods like ChainMap or itertools.chain() for large data sets.
  • Avoid creating unnecessary copies of dictionaries.
  • Optimize performance by choosing the appropriate method based on the Python version and use case.

Merge Dictionaries in Python: Summary and Key Takeaways

  • Merging dictionaries is a common operation in Python that involves combining key-value pairs from multiple dictionaries.
  • Python provides several methods for merging dictionaries, including the update() method, dictionary unpacking, union operators, ChainMap, and `itertools.chain()`.
  • Handling duplicate keys, nested dictionaries, and complex data structures requires careful consideration and custom logic.
  • The choice of method for merging dictionaries depends on the Python version, the size of the data sets, and the desired behavior.

Conclusion: Mastering the Art of Merging Dictionaries in Python

Merging dictionaries is an essential skill in Python programming, enabling us to manipulate and organize data effectively. Throughout this article, we’ve explored various methods for merging dictionaries, addressed common use cases, and provided tips and tricks for handling complex scenarios.

By understanding the different approaches and their trade-offs, you can confidently choose the right method for your specific needs and become proficient in the art of merging dictionaries in Python.