How to merge two dictionaries in python
In this article, we will learn to merge two dictionaries in Python.
Two dictionaries can be merged using the update()
function. No new dictionary is created here, the second dictionary is merged with first one.
d1 = {1 : 'Avani', 2 : 'Karan', 3 : 'Zilufa'} d2 = {'One' : 'red', 'Two' : 'blue', 1:'Avani'} print(d1) print(d2) print() d1.update(d2) print('After merging d2 with d1\n') print(d1) print(d2)
Output:
{1: 'Avani', 2: 'Karan', 3: 'Zilufa'} {'One': 'red', 'Two': 'blue', 1: 'Avani'} After merging d2 with d1 {1: 'Avani', 2: 'Karan', 3: 'Zilufa', 'One': 'red', 'Two': 'blue'} {'One': 'red', 'Two': 'blue', 1: 'Avani'}
Subscribe
Login
Please login to comment
0 Discussion