Data Structures in Python: ChainMap

Satoro
3 min readOct 15, 2021

Hello, I hope you are following this course from the beginning. With today's tutorial, we are heading towards more and more interesting data structures in python.

Links to previous tutorials are available at the bottom of this article

ChainMap is a data type in Python. It is a container that encapsulates many dictionaries into one unit. The combined dictionaries contain keys and values in specific sequences. The best use of ChainMap is to search through multiple dictionaries at a time and get proper key-value pair. It is often much faster than creating a new dictionary and running multiple update() calls.

Are you confused? Don’t worry, let's implement to get a better idea about this.

Creating a ChainMap

First of all, we have to import ChainMap from the ‘collections’ module:

from collections import ChainMap

Now let's create a ChainMap:

d1 = {'day1': 'Mon', 'day2': 'Tue'}
d2 = {'day3': 'Wed', 'day1': 'Thu'}
c_m = ChainMap(d1, d2)print(c_m)#OUTPUT
ChainMap({'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day1': 'Thu'})

In the above example, we created two dictionaries and then stored those in one ChainMap.

Accessing

ChainMap supports all usual dictionary methods. In it has a map() method which is used to display keys with corresponding values of all the dictionaries in ChainMap in a stored state. It is a user updatable list mapping.

Let’s see how to use it:

print(c_m.maps) #here we used maps method on c_m and printed it#OUTPUT
[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day1': 'Thu'}]

Accessing Keys and Values

We can access the list of keys and values using the key() and value() methods in ChainMap.

ks = list(c_m.keys()) # List of keys
print(ks)
#OUTPUT
['day3', 'day1', 'day2']
vs = list(c_m.values()) #List of values
print(vs)
#OUTPUT
['Wed', 'Mon', 'Tue']
#Printing all values
for key, value in c_m.items():
print(f"key: {key}, value: {value}")
#OUTPUT:
key: day3, value: Wed
key: day1, value: Mon
key: day2, value: Tue

Here in the above example of accessing the list of keys and values, you should notice that in the list of keys, a duplicate key is removed, and in the list of values value of that duplicate key is removed.

Updating

When elements in the dictionary are updated then ChainMap is also instantly updated.

d2['day3'] = 'Sun'print(c_m.maps)#OUTPUT
[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Sun', 'day1': 'Thu'}]

That should be enough for this tutorial I hope you enjoyed this tutorial. See you tomorrow, bye!!

Lectures

Lecture 1: https://medium.com/@saifmdco/data-structures-with-python-introduction-4dadeffa2215

Lecture 2: https://medium.com/@saifmdco/data-structures-with-python-arrays-c498518bf7fd

Lecture 3: https://medium.com/@saifmdco/data-structures-in-python-lists-653a3ad103ab

Lecture 4: https://medium.com/@saifmdco/data-structures-in-python-tuples-3c350640cd9a

Lecture 5: https://medium.com/@saifmdco/data-structures-in-python-dictionary-fad27ffdda8b

Lecture 6: https://medium.com/@saifmdco/data-structures-in-python-2d-array-6bc0154aa717

Lecture 7: https://medium.com/@saifmdco/data-structures-in-python-matrix-22adba5aa597

Lecture 8: https://medium.com/@saifmdco/data-structures-in-python-sets-92d5445e97e8

Lecture 9: https://medium.com/@saifmdco/data-structures-in-python-chainmap-fca2a9d47249

Lecture 10: https://medium.com/@saifmdco/data-structures-in-python-linkedlist-50f2118c659e

Lecture 11: https://medium.com/@saifmdco/data-structures-in-python-stack-6bf182d63581

Lecture 12: https://medium.com/@saifmdco/data-structures-in-python-queue-6361d3dcff0

Lecture 13: https://medium.com/@saifmdco/data-structures-in-python-dequeue-1a585e269a55

Lecture 14: https://medium.com/@saifmdco/data-structures-in-python-custom-stack-b7b9173b4eae

Lecture 15: https://medium.com/@saifmdco/data-structures-in-python-hash-table-39be784eefc1

Lecture 16: https://medium.com/@saifmdco/data-structures-in-python-doubly-linked-list-fe698d74756c

--

--