Merge Dictionaries in Python (8 different methods)

In this python tutorial, we will discuss the Python concatenate dictionary.

There are eight ways to merge or concatenate dictionaries in Python, which are shown below:

  1. Using the update() method
  2. Using the double-asterisk notation (unpacking)
  3. Using the dictionary constructor
  4. Using the ChainMap()
  5. Using the union operator (|)
  6. Using chain() method
  7. Using the dictionary comprehension
  8. Using the reduce() function

Python concatenate dictionary

In some cases, you may need to concatenate two or more dictionaries together to create a larger dictionary. There are several ways to concatenate dictionaries in Python, including using the update() method, the ** operator, and the chain() method from the itertools module and etc.

Method-1: Python concatenate dictionary using the update() method

The update() method merges the keys and values of one dictionary into another.

# Define two dictionaries containing different key-value pairs
dictionary1 = {'Pen': 5, 'Pencil': 4, 'Chocolate': 15}
dictionary2 = {'Apple': 25, 'Ball': 10, 'Doll': 20}

# Use the update() method to add the key-value pairs from dictionary2 to dictionary1
dictionary1.update(dictionary2)

# Print the updated dictionary1, which now contains all the key-value pairs from both dictionaries
print(dictionary1)

The above code defines two dictionaries containing different key-value pairs. The code then uses the update() method to add the key-value pairs from dictionary2 to dictionary1.

  • Finally, the code prints the updated dictionary1, which now contains all the key-value pairs from both dictionaries.
Python concatenate dictionaries
Python concatenate dictionaries

Read: Python Dictionary Methods

Method-2: Python concatenate dictionary using the double-asterisk notation (unpacking)

The double-asterisk notation (**dict) can be used to unpack a dictionary and merge it with another.

# Define two dictionaries containing different key-value pairs
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

# Use dictionary unpacking to merge the two dictionaries into a single dictionary
merged_dict = {**dict1, **dict2}

# Print the merged dictionary
print(merged_dict)

The above code defines two dictionaries dict1 and dict2 with different key-value pairs. It then uses dictionary unpacking to merge these two dictionaries into a single dictionary called merged_dict. Finally, it prints the merged dictionary.

Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Read: Python Dictionary sort

Method-3: Python concatenate dictionary using the dictionary constructor

The dictionary constructor can be used to create a new dictionary by merging two or more dictionaries.

# Define two dictionaries containing different key-value pairs
dict1 = {'USA': 1, 'United Kingdom': 2}
dict2 = {'Canada': 3, 'Brazil': 4}

# Use the dict() constructor and dictionary unpacking to merge the two dictionaries into a single dictionary
merged_dict = dict(dict1, **dict2)

# Print the merged dictionary
print(merged_dict)

The above code defines two dictionaries dict1 and dict2 with different key-value pairs.

  • It then uses the built-in dict() constructor along with dictionary unpacking to merge these two dictionaries into a single dictionary called merged_dict.
  • Finally, it prints the merged dictionary.
Output: {'USA': 1, 'United Kingdom': 2, 'Canada': 3, 'Brazil': 4}

Read: Python Dictionary index

Method-4: Python concatenate dictionary using the ChainMap()

The ChainMap() class from the collections module can be used to concatenate dictionaries by creating a mapping of several dictionaries.

# Import the ChainMap class from the collections module
from collections import ChainMap

# Define two dictionaries containing different key-value pairs
dict1 = {'USA': 1, 'United Kingdom': 2}
dict2 = {'Canada': 3, 'Brazil': 4}

# Use the ChainMap class to merge the two dictionaries into a single dictionary
merged_dict = ChainMap(dict1, dict2)

# Convert the merged dictionary into a regular dictionary and print it
print(dict(merged_dict))

The code first imports the ChainMap class from the collections module. It then defines two dictionaries dict1 and dict2 containing different key-value pairs.

  • The ChainMap class is then used to merge these two dictionaries into a single dictionary called merged_dict.
  • Finally, the dict() constructor is used to convert the merged dictionary into a regular dictionary and the resulting dictionary is printed.
Output: {'Canada': 3, 'Brazil': 4, 'USA': 1, 'United Kingdom': 2}

Read: Python dictionary extend

Method-5: Python concatenate dictionary using the union operator (|)

Starting from Python 3.9, you can use the union operator (|) to concatenate dictionaries.

# Define two dictionaries containing different key-value pairs
dict1 = {'USA': 1, 'United Kingdom': 2}
dict2 = {'Canada': 3, 'Brazil': 4}

# Use the union operator to merge the two dictionaries into a single dictionary
merged_dict = dict1 | dict2

# Print the resulting merged dictionary
print(merged_dict)

The code above code defines two dictionaries containing different key-value pairs. The union operator | is then used to merge the two dictionaries into a single dictionary called merged_dict. Finally, the merged dictionary is printed.

Python Concatenate Dictionary Using the Union
Python Concatenate Dictionary Using the Union

Read: Python Dictionary Count

Method-6: Python concatenate dictionary using chain() method

The chain() method from the itertools module can be used to combine multiple iterables into a single iterable. When used with dictionaries, the keys and values from each dictionary are merged into a single iterator, which can be used to create a new dictionary.

# Import the itertools module
import itertools

# Define two dictionaries containing different key-value pairs
dict1 = {'USA': 1, 'United Kingdom': 2}
dict2 = {'Canada': 3, 'Brazil': 4}

# Create an empty dictionary called merged_dict
merged_dict = {}

# Use itertools.chain() to chain the items from both dictionaries, and then loop through them
for key, value in itertools.chain(dict1.items(), dict2.items()):
    merged_dict[key] = value

# Print the resulting merged dictionary
print(merged_dict)

The code imports the itertools module and defines two dictionaries called dict1 and dict2. The program then creates an empty dictionary merged_dict.

  • Using the itertools.chain() function, the code combines the key-value pairs from both dictionaries into a single iterable, which is then looped through with a for loop.
  • The loop assigns the key-value pairs to the key and value variables and adds them to merged_dict.
Output: {'USA': 1, 'United Kingdom': 2, 'Canada': 3, 'Brazil': 4}

Read: Python Dictionary of sets

Method-7: Python concatenate dictionary using the dictionary comprehension

Dictionary comprehension is a concise and readable way to create a dictionary from an iterable. When used with multiple dictionaries, it can be used to merge them into a single dictionary.

# Define two dictionaries with key-value pairs
dict1 = {'USA': 1, 'United Kingdom': 2}
dict2 = {'Canada': 3, 'Brazil': 4}

# Merge the key-value pairs from both dictionaries into a single dictionary using a dictionary comprehension
merged_dict = {k: v for d in (dict1, dict2) for k, v in d.items()}

# Print the resulting merged dictionary
print(merged_dict)

The above code defines two dictionaries dict1 and dict2 and creates a new dictionary merged_dict.

  • It then merges the key-value pairs from both dictionaries into merged_dict using dictionary comprehension. Finally, it prints out the resulting merged_dict.
Output: {'USA': 1, 'United Kingdom': 2, 'Canada': 3, 'Brazil': 4}

Method-8: Python concatenate dictionary using the reduce() function

The reduce() function from the functools module can be used to concatenate a list of dictionaries.

# Import the reduce function from the functools module
from functools import reduce

# Define a list of dictionaries with key-value pairs
dicts = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]

# Use reduce to merge the key-value pairs from all dictionaries into a single dictionary
# The lambda function merges two dictionaries using the unpacking syntax and returns the result
merged_dict = reduce(lambda d1, d2: {**d1, **d2}, dicts)

# Print the resulting merged dictionary
print(merged_dict)

The above code imports the reduce function from the functools module and creates a list of three dictionaries.

  • It then uses the reduce function to merge the dictionaries into a single dictionary. The lambda function takes two dictionaries as inputs and merges them using the unpacking operator **. Finally, the merged dictionary is printed to the console.
Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

Also, take a look at some more Python tutorials.

Conclusion

In this python tutorial, we have learned Python concatenate dictionary using the different approaches:

  • Using the update() method
  • Using the double-asterisk notation (unpacking)
  • Using the dictionary constructor
  • Using the ChainMap()
  • Using the union operator (|)
  • Using chain() method
  • Using the dictionary comprehension
  • Using the reduce() function