Merge dictionaries in Python
Merge dictionaries in Python with update()
address = {'street': 'MG Road', 'house': '123'}
contact = {'country': 'India', 'city': 'Indore', 'street': 'Ring Road'}
address.update(contact)
# Address dictionary will now have the updated data
{'house': '123', 'country': 'India', 'city': 'Indore', 'street': 'Ring Road'}
We can use update() when we are required to merge two dictionaries in Python
address.update(contact), the key-value pairs of contact(dictionary2) will be written into the address(dictionary1).And for keys in both dictionary1 and dictionary2, the value in dictionary1 will be overwritten by the corresponding value in dictionary2
Comments
Post a Comment