Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 9, 2020 06:51
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/56b41e416e0174f11ee36d8fea7b6247 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/56b41e416e0174f11ee36d8fea7b6247 to your computer and use it in GitHub Desktop.
from collections import ChainMap
d1={'a':1,'b':2}
d2={'c':3,'d':4}
chain=ChainMap(d1,d2)
print (chain)#Output:ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4})
#using new_child attirbute.It will add the new map in front of the list of all mappings.
d3=chain.new_child(m={'e':5})
print (d3)#Output:ChainMap({'e': 5}, {'a': 1, 'b': 2}, {'c': 3, 'd': 4})
#Return type is ChainMap object
print (type(d3))#Output:<class 'collections.ChainMap'>
#if m (new map) is not specified, it will add empty dictionary in the front of all mappings.
d4=chain.new_child()
print (d4)#Output:ChainMap({}, {'a': 1, 'b': 2}, {'c': 3, 'd': 4})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment