{"id":25927,"date":"2021-12-16T09:20:40","date_gmt":"2021-12-16T03:50:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=25927"},"modified":"2021-12-16T09:20:40","modified_gmt":"2021-12-16T03:50:40","slug":"python-collections-chainmap-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-collections-chainmap-method-with-examples\/","title":{"rendered":"Python collections ChainMap() method with Examples"},"content":{"rendered":"
ChainMap():<\/strong><\/p>\n Python has a container called “ChainMap” that combines many dictionaries into a single unit. ChainMap is part of the “collections” module.<\/p>\n Access Operations on ChainMap():<\/strong><\/p>\n keys():<\/strong><\/p>\n values():<\/strong><\/p>\n maps():<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n this<\/strong><\/p>\n this<\/strong><\/p>\n 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 …<\/p>\n This function displays all of the keys from all of the dictionaries in ChainMap.<\/pre>\n
This function displays the values of all dictionaries in ChainMap.<\/pre>\n
This function is used to display all of the dictionaries in ChainMap's keys and values.<\/pre>\n
Collections ChainMap() method with Examples in Python<\/h2>\n
i)Implementing ChainMap<\/h3>\n
\n
# Import the chainMap function from collections using the import keyword.\r\nfrom collections import ChainMap\r\n# Give the dictionary with some random keys and values and store it in a variable.\r\ndictnry1 = {'Good': 100, 'Morning': 448}\r\n# Repeat the previous step for as many dictionaries as you choose.\r\ndictnry2 = {'hello': 1300, 'this': 1448}\r\ndictnry3 = {'is': 1070, 'Python-Programs': 4448}\r\n# Pass all the above dictionaries as arguments to chainMap() function\r\n# and store it in a variable.\r\nreslt = ChainMap(dictnry1, dictnry2, dictnry3)\r\n# Print the Above Result.\r\nprint('The final Result after applying ChainMap : ')\r\nprint(reslt)\r\n<\/pre>\n
The final Result after applying ChainMap : \r\nChainMap({'Good': 100, 'Morning': 448}, {'hello': 1300, 'this': 1448}, {'is': 1070, 'Python-Programs': 4448})<\/pre>\n
ii)Implementing ChainMap Access Operations<\/h4>\n
\n
# Import the chainMap function from collections using the import keyword.\r\nfrom collections import ChainMap\r\n# Give the dictionary with some random keys and values and store it in a variable.\r\ndictnry1 = {'hello': 100, 'this': 200}\r\n# Repeat the previous step for as many dictionaries as you choose.\r\ndictnry2 = {'this': 300, 'python-programs': 400}\r\n# Pass all the above dictionaries as arguments to chainMap() function\r\n# and store it in a variable say chainRslt.\r\nchainRslt = ChainMap(dictnry1, dictnry2)\r\n# Print the ChainMaps using the maps function.\r\n# Apply the maps method for the above result(chain) and print it.\r\nprint('The Total Contents in the Chain Map are : ')\r\nprint(chainRslt.maps)\r\n\r\n# To get All the keys in the chain we use the keys() method.\r\n# Apply the keys() function for the chainRslt\r\n# and convert it to a list using the list() function.\r\n# Print the Above keys() list.\r\nprint('The keys present in Chain Map are : ')\r\nprint(list(chainRslt.keys()))\r\n# To get All the values\u00a0 in the chain we use the values() method.\r\n# Apply the values () function for the chainRslt\u00a0and convert it to a list using the list() function.\r\n# Print the Above values() list.\r\nprint('The values present in Chain Map are : ')\r\nprint(list(chainRslt.values()))\r\n<\/pre>\n
The Total Contents in the Chain Map are : \r\n[{'hello': 100, 'this': 200}, {'this': 300, 'python-programs': 400}]\r\nThe keys present in Chain Map are : \r\n['python-programs', 'this', 'hello']\r\nThe values present in Chain Map are : \r\n[400, 200, 100]<\/pre>\n
The key \"<\/pre>\n
\" exists in both dictionaries, but only the first dictionary key\r\nis used as the key value of \"<\/pre>\n
\". The dictionaries are ordered as they are\r\nprovided into the function.<\/pre>\n","protected":false},"excerpt":{"rendered":"