This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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