Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

chainmap
python
communitycreator

What is ChainMap collection in Python?

Harsh Jain

ChainMap is a data structure that encapsulates multiple dictionaries into one. It can be imported from the Python standard library collections. At times, it might so happen that we are working with multiple dictionaries and we need to group and manage them as a single one. In such cases, Chainmap comes to our rescue.

Description

ChainMap, although it is a collection of multiple dictionaries, behaves like a single dictionary. ChainMap can be used for the following processes.

  • We can easily perform search operation in multiple dictionaries.

  • We can create a chain of some default values.

  • We can perform operations that compute some subsets of a dictionary.

Let’s understand this with an example.

Let’s say there is a store that sells only three categories of items:

  • Cars
  • Bikes
  • Clothing

Now, each category has numerous items and can be represented by a dictionary. But it might still be a cumbersome job for the store-keeper to account for items across different categories. In such a scenario, a ChainMap may be used to account for the different categories. That is, the ChainMap will be a dictionary of dictionaries.

Code

Let’s have a look at the code to understand the concept of ChainMaps.

# Normal Dictionaries
cars = {"Audi": 30, "BMW": 20}
bikes = {"Yamaha": 1000, "Hero": 800, "Others": 400}
mobiles = {"Apple": 40, "Samsung": 10}

# Using ChainMap
from collections import ChainMap
inventory = ChainMap(cars, bikes, mobiles)

# Displaying Inventory
print(inventory)

# Querying Toys
print(cars["Audi"])

# Querying Inventory
print(inventory["Audi"])

# adding a new item in Inventory
apparel = {"Shirt": 40, "Trousers":30}
new_inventory = inventory.new_child(apparel)
print(new_inventory["Trousers"])

# deleting an object
new_inventory.pop("Trousers")
# print(new_inventory["Blocks"])
Use ChainMaps in Python

Explanation

  • From lines 2 to 4, we have initiated three different dictionaries containing items of different categories, namely toys, computers and clothing.

  • In line 7, we have imported the ChainMap module.

  • In line 8, we have created a ChainMap which stores all the three dictionaries together.

  • In line 11, we print the ChainMap that basically contains all the three dictionaries.

  • In line 14, we have queried the dictionary toys to check how many Monopoly games are present and we get 20 as the output.

  • In line 17, we queried the ChainMap inventory to check the same and we got the exact same output which confirms that the ChainMap can be indeed used effectively instead of three different dictionaries.

  • In line 20, we create a new dictionary object that contains data for apparel related products.

  • In line 21, we use the new_child() function and pass the new dictionary to add new data to our ChainMap collection.

  • In line 22, we print the ChainMap by querying it with the apparel’s data.

  • In line 25, we used the pop() function to delete a dictionary object from the ChainMap.

  • Now, if we un-comment in line 26, we will get an error that the key is not present in the ChainMap.

In this way, a ChainMap can be effectively used when we are dealing with multiple dictionaries simultaneously.

Note: All operations of a dictionary can be well applied to a ChainMap.

RELATED TAGS

chainmap
python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring