Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 9, 2020
Embed
What would you like to do?
from collections import ChainMap
d1={'a':1,'b':2,'c':3}
d2={'d':4,'e':5}
d3=ChainMap(d1,d2)
print (d3)#Output:ChainMap({'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5})
#Adding value in first mapping.It will update values only in first mapping.
d3['e']=99
print (d3)#Output:ChainMap({'a': 1, 'b': 2, 'c': 3, 'e': 99}, {'d': 4, 'e': 5})
#deleting value.It will delete values only from first mapping.Since 'd' is not in first mapping,it raises key Error
#del d3['d']
#Output:KeyError: "Key not found in the first mapping: 'd'"
#Accessing values,it will lookup the full chain.
print (d3['d'])#Output:4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment