Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 9, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/85f1ad82782cbb01f16c98d0d44b6421 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/85f1ad82782cbb01f16c98d0d44b6421 to your computer and use it in GitHub Desktop.
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