×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Merge two dictionaries in a single expression in Python

By Sapna Deraje Radhakrishna Last updated : December 21, 2024

There can be multiple approaches to merge two or more Python dictionaries such as by using the update(), merge(), collections.ChainMap(), itertools.chain() methods. In this tutorial, we will learn a single-line approach to merge two or more dictionaries.

Merge two dictionaries in a single expression

With python 3.5+, the below approach is generally followed to merge two (or more) dictionaries.

Example

# Merge two dictionaries in a
# single expression

# Dictionaries
dict_1 = {"x": 1, "y": 2}
dict_2 = {"a": 3, "b": 4}

# Printing dictionaries
print("Original dictionaries...")
print("dict_1:", dict_1)
print("dict_2:", dict_2)

# Merging dictionaries
merged_dict = {**dict_1, **dict_2}

# Printing merged dictionary
print("Merged dictionary...")
print(merged_dict)

Output

Original dictionaries...
dict_1: {'x': 1, 'y': 2}
dict_2: {'a': 3, 'b': 4}
Merged dictionary...
{'x': 1, 'y': 2, 'a': 3, 'b': 4}

Merge more than two dictionaries in a single line

The above can be used to merge two or more dictionaries, like below,

Example

# Merge more than two dictionaries in a
# single expression

# Dictionaries
dict_1 = {"x": 1, "y": 2}
dict_2 = {"a": 3, "b": 4}
dict_3 = {"p": 5, "q": 6}

# Printing dictionaries
print("Original dictionaries...")
print("dict_1:", dict_1)
print("dict_2:", dict_2)
print("dict_3:", dict_3)

# Merging dictionaries
merged_dict = {**dict_1, **dict_2, **dict_3}

# Printing merged dictionary
print("Merged dictionary...")
print(merged_dict)

Output

Original dictionaries...
dict_1: {'x': 1, 'y': 2}
dict_2: {'a': 3, 'b': 4}
dict_3: {'p': 5, 'q': 6}
Merged dictionary...
{'x': 1, 'y': 2, 'a': 3, 'b': 4, 'p': 5, 'q': 6}

However, since most of the organizations have not yet moved to the latest version of Python, the general approach followed is as mentioned in the initial example.

Merge two dictionaries in a single line (version less than 3.4)

Until version less than 3.4, in python, it was quite a task (no single line approach available) to merge two dictionaries in a single expression. For instance, in version less than 3.4 we had to do something similar (as below) to merge two dictionaries.

def merge(dict_1, dict_2):
    z = dict_1.copy()   
    z.update(dict_2)    
    return z
    
if __name__ == "__main__":
    dict_1 = {'x':1, 'y':2}
    dict_2 = {'a':3, 'b':4}
    merged_dict = merge(dict_1, dict_2)
    print(merged_dict)

Output

{'x': 1, 'y': 2, 'a': 3, 'b': 4}

In the above example, (ref merge method), the dict_1 is shallow copied to a different variable called z, and the z is then updated with the values of dict_2, which results in the merging of two dictionaries.

Reference: stackoverflow

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.