How To Merge Two Dictionaries In Python?

Merging dictionaries is a common operation in Python, and there are multiple approaches to combine the key-value pairs from two dictionaries.

In this guide, we'll explore various methods for merging dictionaries, covering both Python 3.5 and later versions.

Method 1: Using the update() Method

The update() method is a straightforward way to merge two dictionaries. It updates the dictionary with elements from another dictionary or from an iterable of key-value pairs.

# Example dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Using update() to merge dictionaries
dict1.update(dict2)

# Display the merged dictionary
print("Merged Dictionary:", dict1)

The output will be:

Merged Dictionary: {'a': 1, 'b': 3, 'c': 4}

In this example, the values from dict2 have updated the corresponding values in dict1.

If there are common keys, the values from the second dictionary overwrite the values in the first dictionary.

Method 2: Using Dictionary Unpacking (**)

Dictionary unpacking, introduced in Python 3.5, provides a concise way to merge dictionaries using the ** operator.

# Example dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Using dictionary unpacking to merge dictionaries
merged_dict = {**dict1, **dict2}

# Display the merged dictionary
print("Merged Dictionary:", merged_dict)

The output will be the same as in the previous example:

Merged Dictionary: {'a': 1, 'b': 3, 'c': 4}

Here, the **dict1 unpacks the key-value pairs from dict1, and **dict2 unpacks the key-value pairs from dict2. The resulting dictionary contains the merged key-value pairs.

Method 3: Using dict() Constructor

The dict() constructor can be used to merge dictionaries by passing iterable key-value pairs as arguments.

# Example dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Using dict() constructor to merge dictionaries
merged_dict = dict(list(dict1.items()) + list(dict2.items()))

# Display the merged dictionary
print("Merged Dictionary:", merged_dict)

The output will be the same as in the previous examples:

Merged Dictionary: {'a': 1, 'b': 3, 'c': 4}

In this method, the items() method is used to obtain the key-value pairs from each dictionary, and the + operator concatenates the two lists of key-value pairs.

Method 4: Using Dictionary Comprehension

Dictionary comprehension can also be employed to merge dictionaries, providing a concise and expressive syntax.

# Example dictionaries to merge
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Using dictionary comprehension to merge dictionaries
merged_dict = {key: value for d in [dict1, dict2] for key, value in d.items()}

# Display the merged dictionary
print("Merged Dictionary:", merged_dict)

The output will be the same as in the previous examples:

Merged Dictionary: {'a': 1, 'b': 3, 'c': 4}

In this example, the nested comprehension iterates over the dictionaries, extracting key-value pairs using items().

Conclusion:

Merging dictionaries in Python can be achieved through various methods, each offering different levels of simplicity and readability.

Whether you prefer the classic update() method, the concise dictionary unpacking, the dict() constructor, or the expressive dictionary comprehension, understanding these approaches empowers you to choose the most suitable method for your specific scenario.

As you work with dictionaries in your Python projects, the ability to merge and combine key-value pairs efficiently becomes a valuable skill. Happy coding!