Python collections ChainMap() method with Examples

ChainMap():

Python has a container called “ChainMap” that combines many dictionaries into a single unit. ChainMap is part of the “collections” module.

Access Operations on ChainMap():

keys():

 This function displays all of the keys from all of the dictionaries in ChainMap.

values():

 This function displays the values of all dictionaries in ChainMap.

maps():

 This function is used to display all of the dictionaries in ChainMap's keys and values.

Collections ChainMap() method with Examples in Python

i)Implementing ChainMap

Approach:

  • Import the chainMap function from collections using the import keyword.
  • Give the dictionary with some random keys and values and store it in a variable.
  • Repeat the previous step for as many dictionaries as you choose.
  • Pass all the above dictionaries as arguments to chainMap() function and store it in a variable.
  • Print the Above Result.
  • The Exit of the Program.

Below is the Implementation:

# Import the chainMap function from collections using the import keyword.
from collections import ChainMap
# Give the dictionary with some random keys and values and store it in a variable.
dictnry1 = {'Good': 100, 'Morning': 448}
# Repeat the previous step for as many dictionaries as you choose.
dictnry2 = {'hello': 1300, 'this': 1448}
dictnry3 = {'is': 1070, 'Python-Programs': 4448}
# Pass all the above dictionaries as arguments to chainMap() function
# and store it in a variable.
reslt = ChainMap(dictnry1, dictnry2, dictnry3)
# Print the Above Result.
print('The final Result after applying ChainMap : ')
print(reslt)

Output:

The final Result after applying ChainMap : 
ChainMap({'Good': 100, 'Morning': 448}, {'hello': 1300, 'this': 1448}, {'is': 1070, 'Python-Programs': 4448})

ii)Implementing ChainMap Access Operations

Approach:

  • Import the chainMap function from collections using the import keyword.
  • Give the dictionary with some random keys and values and store it in a variable.
  • Repeat the previous step for as many dictionaries as you choose.
  • Pass all the above dictionaries as arguments to chainMap() function and store it in a variable say chainRslt.
  • Print the ChainMaps using the maps function.
  • Apply the maps method for the above result(chain) and print it.
  • To get All the keys in the chain we use the keys() method.
  • Apply the keys() function for the chainRslt and convert it to a list using the list() function.
  • Print the Above keys() list.
  • To get All the values  in the chain we use the values() method.
  • Apply the values () function for the chainRslt and convert it to a list using the list() function.
  • Print the Above values() list.
  • The Exit of the Program.

Below is the Implementation:

# Import the chainMap function from collections using the import keyword.
from collections import ChainMap
# Give the dictionary with some random keys and values and store it in a variable.
dictnry1 = {'hello': 100, 'this': 200}
# Repeat the previous step for as many dictionaries as you choose.
dictnry2 = {'this': 300, 'python-programs': 400}
# Pass all the above dictionaries as arguments to chainMap() function
# and store it in a variable say chainRslt.
chainRslt = ChainMap(dictnry1, dictnry2)
# Print the ChainMaps using the maps function.
# Apply the maps method for the above result(chain) and print it.
print('The Total Contents in the Chain Map are : ')
print(chainRslt.maps)

# To get All the keys in the chain we use the keys() method.
# Apply the keys() function for the chainRslt
# and convert it to a list using the list() function.
# Print the Above keys() list.
print('The keys present in Chain Map are : ')
print(list(chainRslt.keys()))
# To get All the values  in the chain we use the values() method.
# Apply the values () function for the chainRslt and convert it to a list using the list() function.
# Print the Above values() list.
print('The values present in Chain Map are : ')
print(list(chainRslt.values()))

Output:

The Total Contents in the Chain Map are : 
[{'hello': 100, 'this': 200}, {'this': 300, 'python-programs': 400}]
The keys present in Chain Map are : 
['python-programs', 'this', 'hello']
The values present in Chain Map are : 
[400, 200, 100]

Note:

The key "

this

" exists in both dictionaries, but only the first dictionary key
is used as the key value of "

this

". The dictionaries are ordered as they are
provided into the function.