ChainMap in Python



Operations on ChainMap

Operations Access

1. keys (): — this function is used to display all keys of all dictionaries in ChainMap.

2. values ​​(): — This function is used to display the values ​​ of all dictionaries in ChainMap.

3. maps: — This function is used to display keys with matching values ​​ of all dictionaries in ChainMap.

# Please select Python 3 to run this code in the IDE
# Python code to demonstrate ChainMap and
# keys (), values ​​() and maps

 
# import collections for ChainMap operations

import collections

 
# initializing dictionaries

dic1 = { `a` : 1 , `b` : 2 }

dic2 = { `b` : 3 , `c` : 4 }

 
# initializing ChainMap

chain = collections .ChainMap (dic1, dic2)

 
# printing a chainMap using maps

print ( "All the ChainMap contents are : " )  

print (chain.maps)

 
# print keys using () keys

print ( "All keys of ChainMap are:" )

print ( list (chain.keys ()))

 
# print keys using keys ()

print ( "All values ​​of ChainMap are: " )

print ( list (chain.values ​​()))

You turn:

 All the ChainMap contents are: [{`b`: 2,` a`: 1}, {`c`: 4,` b`: 3}] All keys of ChainMap are: [`a`,` c`, `b`] All values ​​of ChainMap are: [1, 4, 2] 

Note. Please note that a key named “b” exists in both dictionaries, but only the first key of the dictionary is accepted as the value of the key “b”. Ordering is done as dictionaries are passed to functions.

Manipulating Operations

1. new_child (): — This function adds a new dictionary to the beginning of the ChainMap.

2. reversed (): — This function reverses the relative order of dictionaries in ChainMap.

Output:

 All the ChainMap contents are: [{`b`: 2,` a`: 1}, {`b` : 3, `c`: 4}] Displaying new ChainMap: [{` f`: 5}, {`b`: 2,` a`: 1}, {`b`: 3,` c`: 4} ] Value associated with b before reversing is: 2 Value associated with b after reversing is: 3 

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.


# Please select Python 3 to run this code in IDE
# Python code to demonstrate ChainMap and
# reversed () and new_child ()

 
# import collections for ChainMap operations

import collections

 
# initializing dictionaries

dic1 = { `a` : 1 , `b` : 2 }

dic2 = { ` b` : 3 , `c` : 4 }

dic3 = { `f` : 5 }

 
# initializing ChainMap

chain = collections.ChainMap (dic1, dic2)

 
# print a chain Map using a map

print ( "All the ChainMap contents are: " )

print (chain.maps )

 
# using new_child () to add a new dictionary

chain1 = chain.new_child (dic3)

 
# chain printing Map using map

print ( "Displaying new ChainMap:" )

print (chain1.maps)

 
# displays the value associated with b before o by reference

print ( "Value associated with b before reversing is: " , end = " ")

print (chain1 [ `b` ])

 
# reverse ChainMap

chain1.maps = reversed (chain1.maps)

 
# display the value associated with b after reverse

print ( "Value associated with b after reversing is:" , end = < / code> "")

print (chain1 [ `b` ])