ChainMap in Python

ChainMap is a standard library in the “collections” module. ChainMap in Python helps quickly linking a number of mappings to create a single unit. The mappings can be accessed using maps attribute. The mappings are stored in a list.
Keep in mind that the list should contain at least one mapping.

Properties and Methods on ChainMap in Python

  • maps
    A list of key-value pairs, which can be updated or modified by the user. It should contain at least one mapping. keys() displays all the keys, while values() displays all the values in all dictionaries.
import collections 

#defining the dictionaries
p = { 'mayank' : 1, 'abhinav' : 2 } 
q = { 'jai' : 3, 'vijay' : 4 } 

#defining ChainMap
chainmap = collections.ChainMap(p,q) 
  
print ("Keys and values are: ") 
print (chainmap.maps) 
  
#printing all keys
print (list(chainmap.keys())) 
  
#printing all values
print (list(chainmap.values()))

And the output is as shown:

Keys and values are:

[{'mayank': 1, 'abhinav': 2}, {'jai': 3, 'vijay': 4}]

['jai', 'vijay', 'mayank', 'abhinav']

[3, 4, 1, 2]
  • new_child
    It gives in return, a new ChainMap with a new dictionary at the beginning, followed by all the maps(dictionaries) from the previous chainMap.

    import collections 
    
    #defining the dictionaries
    p = { 'mayank' : 1, 'abhinav' : 2 } 
    q = { 'mayank' : 3, 'vijay' : 4 } 
    r = {'hari' : 0}
    
    #defining ChainMap
    chainmap = collections.ChainMap(p,q) 
      
    print ("Keys and values are: ") 
    print (chainmap.maps) 
      
    # using new_child()
    chainmap1 = chainmap.new_child(r) 
      
    # printing chainmap1 
    print (chainmap1.maps) 
      
    # value of 'mayank' before reversing
    print ("Value of b before reversing : ",end="") 
    print (chainmap1['mayank']) 
      
    # applying reverse operation 
    chainmap1.maps = reversed(chainmap1.maps) 
      
    # value of 'mayank' after reversing
    print ("Value of b after reversing : ",end="") 
    print (chainmap1['mayank'])

    And the output is as shown below:

    Keys and values are:
    
    [{'mayank': 1, 'abhinav': 2}, {'jai': 3, 'vijay': 4}]
    
    ['jai', 'vijay', 'mayank', 'abhinav']
    
    [3, 4, 1, 2]
  • parents
    new_child adds a new map to the existing chainMap, while parents skip (removes) the first map.

    import collections 
    
    #defining the dictionaries
    p = { 'mayank' : 1, 'abhinav' : 2 } 
    q = { 'mayank' : 3, 'vijay' : 4 } 
    
    #defining ChainMap
    chainmap = collections.ChainMap(p,q) 
      
    print ("Keys and values are: ") 
    print (chainmap.maps) 
      
    # using new_child()
    chainmap1 = chainmap.parents
      
    # printing chainmap1 
    print (chainmap1.maps)

 

And the output will be as shown below.

Keys and values are: 
[{'mayank': 1, 'abhinav': 2}, {'mayank': 3, 'vijay': 4}]
[{'mayank': 3, 'vijay': 4}]

Also read: Python map() function

Leave a Reply

Your email address will not be published. Required fields are marked *