Python ChainMap – All you need to know!

Python Chainmap

Hello, readers! In this article, we will be focusing on Python ChainMap in detail.

So, let us get started!! 🙂


What is Python ChainMap data structure?

Before diving into the concept of ChainMap, let us quickly brush up on our understanding of the Python Collections module.

Python Collections module offers us various data structures to store and manipulate the data. By offering customized storage and manipulation of data, it stands out from all the other default data structures.

The module behaves as a container that stores data objects of different types altogether with a custom feature for molding it into a customized data structure.

One such container offered by the Collections module is ChainMap!

With ChainMap, we can merge and store different types of key-value entities into a single place. The ChainMap container enables us to have multiple dictionaries and then merge them as a single dictionary.

Importing Python ChainMap

In order to implement the functions of the ChainMap module, we need to import the module as shown below–

from collections import ChainMap

Having done this, let us now try to create a generic ChainMap structure from multiple dictionaries

Syntax:

ChainMap(dict, dict)

Example:

In the below example, we have created two dictionaries A and B, and then we have merged both of them to represent them as a single entity.

Output:

from collections import ChainMap 
 
a = {'Age': 1, 'Name': 'X'} 
b = {'Age': 3, 'Name': 'Y'} 
cm = ChainMap(a, b) 
 
print(cm)

Output–

ChainMap({'Age': 1, 'Name': 'X'}, {'Age': 3, 'Name': 'Y'})

Important Functions to Know from the ChainMap module

With ChainMap container comes a huge list of functions that one can use to manipulate the dictionaries stored into it. Let’s take a look at the below functions.

  • keys()
  • values()
  • maps attribute

1. Chainmap.keys() function

As the name suggests, the keys() function enables us to extract the values of the keys out of every key-value pair per dictionary. The keys can be extracted from multiple dictionaries at once.

Syntax–

ChainMap.keys()

Example–

from collections import ChainMap 
 
a = {'Age': 1, 'Name': 'X'} 
b = {'Age': 3, 'Name': 'Y'} 
cm = ChainMap(a, b) 
 
print(cm)
print ("Keys: ") 
print (list(cm.keys())) 

Output–

ChainMap({'Age': 1, 'Name': 'X'}, {'Age': 3, 'Name': 'Y'})
Keys:
['Age', 'Name'] 

2. ChainMap.maps attribute

To have a clearer output, the maps attribute enables us to associate every key to its value from the dictionaries. As a result, it represents the output as every key-value pair of the dictionaries.

Syntax–

ChainMap.maps

Example–

from collections import ChainMap 
 
a = {'Age': 1, 'Name': 'X'} 
b = {'Age': 3, 'Name': 'Y'} 
cm = ChainMap(a, b) 
 
print(cm)
print ("Maps: ")
print (list(cm.maps))

Output–

ChainMap({'Age': 1, 'Name': 'X'}, {'Age': 3, 'Name': 'Y'})
Maps:
[{'Age': 1, 'Name': 'X'}, {'Age': 3, 'Name': 'Y'}]

3. Chainmap.values() function

Apart from displaying the keys and the entire dictionary as a map, the values() function enables us to extract and represent all the values associated with the particular keys independently.

Syntax–

ChainMap.values()

Example–

from collections import ChainMap 
 
a = {'Age': 1, 'Name': 'X'} 
b = {'Std': 3} 
cm = ChainMap(a, b) 
 
print(cm)
print ("Values: ") 
print (list(cm.values()))

Output–

ChainMap({'Age': 1, 'Name': 'X'}, {'Std': 3})
Values: 
[1, 3, 'X']

Conclusion

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

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 🙂