ChainMap in Python – A detailed guide

Filed Under: Python Modules
ChainMap Python

Hello, readers. In this article, we will be focusing on ChainMap in Python. So, let us begin!


First, what is a ChainMap?

Python Collection module provides us with various containers to deal with and manipulate the data. ChainMap is one such container.

Before diving deep into containers, let us understand them.

What are containers?

Containers are objects that have the capacity to store different elements(objects) into them. Thus, Containers serve as a data structure and provides us different ways to store and access the data objects in it.

What is a ChainMap?

ChainMap is a container that merges different dictionaries into a single unit. Thus, it is useful when we have different dictionaries with various key/value pairs, in such cases, the ChainMap represents the dictionaries as a single dictionary.

The collections module provides us with various containers such as ChainMap, OrderedDict, Tuple, etc. In order to use ChainMap into the script, we need to import it through the collections module.

from collections import ChainMap

Let’s create a ChainMap now!

Having understood about ChainMap, let us now create one such ChainMap.

Syntax:

from collections import ChainMap 
ChainMap(dict, dict)

We here need to pass the dictionaries to the ChainMap() function to create a container out of these in a merged/combined form.

Example:

from collections import ChainMap 

x = {'Age': 10, 'Name': 'Vin'} 
y = {'Age': 31, 'Name': 'John'} 

chain_map = ChainMap(x, y) 

print(chain_map) 

In this example, we have created two dictionaries ‘x’ and ‘y’. Further, we have created a ChainMap of the same.

Output:

ChainMap({'Age': 10, 'Name': 'Vin'}, {'Age': 31, 'Name': 'John'})

Functions associated by a Python ChainMap

There are various operations that can be performed through a ChainMap as shown below:

  • keys()
  • values()
  • maps()
  • new_child()
  • reversed(), etc.

Let us have a look at some of them one by one.


1. Python ChainMap.keys()

The keys() function enables us to extract the key values from the multiple dictionaries through a ChainMap.

Syntax:

ChainMap.keys()

Example:

from collections import ChainMap 

x = {'Age': 10, 'Name': 'Vinii'} 
y = {'Age': 31, 'Gender': 'Female'} 

chain_map = ChainMap(x, y)
print("Chainmap: ",chain_map)
print ("Keys of the Chainmap: ") 
print (list(chain_map.keys())) 

As seen above, the keys() function has extracted all the keys from the two dictionaries x and y.

Output:

Chainmap:  ChainMap({'Age': 10, 'Name': 'Vinii'}, {'Age': 31, 'Gender': 'Female'})
Keys of the Chainmap: 
['Name', 'Age', 'Gender']

2. Python ChainMap.values():

The values() function enables us to fetch and display all the value associated with the keys from a ChainMap.

Syntax:

ChainMap.values()

Example:

from collections import ChainMap 

x = {'Age': 10, 'Name': 'Vinii'} 
y = {'Gender': 'Female'} 

chain_map = ChainMap(x, y)
print("Chainmap: ",chain_map)
print ("Values associated with the keys of Chainmap: ") 
print (list(chain_map.values())) 

Output:

As seen below, all the values associated with each key in the ChainMap gets displayed.

Chainmap:  ChainMap({'Age': 10, 'Name': 'Vinii'}, {'Gender': 'Female'})
Values associated with the keys of Chainmap: 
['Vinii', 10, 'Female']

3. Python ChainMap.maps() function:

The maps() function associates every key with its value and represents the corresponding key value pairs.

Thus, the maps() function represents the key value pairs altogether as output.

Syntax:

ChainMap.maps

Example:

from collections import ChainMap 

x = {'Age': 10, 'Name': 'Vinii'} 
y = {'Gender': 'Female'} 

chain_map = ChainMap(x, y)
print ("Values associated with the keys of Chainmap: ") 
print (list(chain_map.maps)) 


Output:

Values associated with the keys of Chainmap: 
[{'Age': 10, 'Name': 'Vinii'}, {'Gender': 'Female'}]

4. Python ChainMap.new_child():

The new_child() function enables us to add a dictionary to the previous ChainMap.

Thus, a new dictionary gets added to the beginning of the ChainMap created from the previous dictionaries.

Syntax:

ChainMap.new_child(dict)

Example:

from collections import ChainMap 

x = {'Age': 10, 'Name': 'Vinii'} 
y = {'Gender': 'Female'} 
chain = {'City':'Pune'}
chain_map = ChainMap(x, y)
print ("Values associated with the keys of Chainmap: ") 
new_chain = chain_map.new_child(chain)
print (list(new_chain.maps))

In this example, we have added the dictionary ‘chain’ to the beginning of the ChainMap created for the dictionaries ‘x’ and ‘y’.

Output:

Values associated with the keys of Chainmap: 
[{'City': 'Pune'}, {'Age': 10, 'Name': 'Vinii'}, {'Gender': 'Female'}]

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, incase you come across any question.

For a deep understanding about this topic, try creating dictionaries and then create a ChainMap out of it and give it a shot, try performing all of the above operations. Do let us know about your experience in the comment section.

For more such posts related to Python, Stay tuned and till then Happy Learning!! 馃檪


References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages