ChainMap in Python - GeeksforGeeks

28 Jun, 2021
Python contains a container called “ChainMap” which encapsulates many dictionaries into one unit. ChainMap is member of module “collections“.
Example:
# Python program to demonstrate  # ChainMap                from collections import ChainMap                d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d3 = {'e': 5, 'f': 6}     # Defining the chainmap  c = ChainMap(d1, d2, d3)         print(c)
Output:
ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6})

Let’s see various Operations on ChainMap
keys() :- This function is used to display all the keys of all the dictionaries in ChainMap.
values() :- This function is used to display values of all the dictionaries in ChainMap.
maps() :- This function is used to display keys with corresponding values of all the dictionaries in ChainMap.
# Please select Python 3 for running this code in IDE# Python code to demonstrate ChainMap and# keys(), values() and maps  # importing collections for ChainMap operationsimport collections  # initializing dictionariesdic1 = { 'a' : 1, 'b' : 2 }dic2 = { 'b' : 3, 'c' : 4 }  # initializing ChainMapchain = collections.ChainMap(dic1, dic2)  # printing chainMap using mapsprint ("All the ChainMap contents are : ")print (chain.maps)  # printing keys using keys()print ("All keys of ChainMap are : ")print (list(chain.keys()))  # printing keys using keys()print ("All values of ChainMap are : ")print (list(chain.values()))
Output :
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 : Notice the key named “b” exists in both dictionaries, but only first dictionary key is taken as key value of “b”. Ordering is done as the dictionaries are passed in function.
new_child() :- This function adds a new dictionary in the beginning of the ChainMap.
reversed() :- This function reverses the relative ordering of dictionaries in the ChainMap.
# Please select Python 3 for running this code in IDE# Python code to demonstrate ChainMap and# reversed() and new_child()  # importing collections for ChainMap operationsimport collections  # initializing dictionariesdic1 = { 'a' : 1, 'b' : 2 }dic2 = { 'b' : 3, 'c' : 4 }dic3 = { 'f' : 5 }  # initializing ChainMapchain = collections.ChainMap(dic1, dic2)  # printing chainMap using mapprint ("All the ChainMap contents are : ")print (chain.maps)  # using new_child() to add new dictionarychain1 = chain.new_child(dic3)  # printing chainMap using mapprint ("Displaying new ChainMap : ")print (chain1.maps)  # displaying value associated with b before reversingprint ("Value associated with b before reversing is : ",end="")print (chain1['b'])  # reversing the ChainMapchain1.maps = reversed(chain1.maps)  # displaying value associated with b after reversingprint ("Value associated with b after reversing is : ",end="")print (chain1['b'])
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 is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python collections-module
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()