How to Merge Dictionaries in Python

Admin
0

For example, you want to merge dictionaries x and y. There are different ways to merge them. Check them out !

1. Python 2, or lower than Python 3.4 you can use a custom function as provided below :

def merge_dicts(x, y):
    z = x.copy()   
    z.update(y)    
    return z

then, called it by :

z = merge_dicts(x, y)

2. For Python 3.5 or greater user :

z = {**x, **y}

3. For Python 3.9.0 or greater :

z = x | y


Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Disclaimer : Content provided on this page is for general informational purposes only. We make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top