Change language

ChainMap in Python

|

Operations on ChainMap

Operations Access

1. keys (): — this function is used to display all keys of all dictionaries in ChainMap.

2. values ​​(): — This function is used to display the values ​​ of all dictionaries in ChainMap.

3. maps: — This function is used to display keys with matching values ​​ of all dictionaries in ChainMap.

# Please select Python 3 to run this code in the IDE
# Python code to demonstrate ChainMap and
# keys (), values ​​() and maps

 
# import collections for ChainMap operations

import collections

 
# initializing dictionaries

dic1 = { ’a’ : 1 , ’b’ : 2 }

dic2 = { ’b’ : 3 , ’c’ : 4 }

 
# initializing ChainMap

chain = collections .ChainMap (dic1, dic2)

 
# printing a chainMap using maps

print ( "All the ChainMap contents are : " )  

print (chain.maps)

 
# print keys using () keys

print ( "All keys of ChainMap are:" )

print ( list (chain.keys ()))

 
# print keys using keys ()

print ( "All values ​​of ChainMap are: " )

print ( list (chain.values ​​()))

You turn:

 All the ChainMap contents are: [{’b’: 2,’ a’: 1}, {’c’: 4,’ b’: 3}] All keys of ChainMap are: [’a’,’ c’, ’b’] All values ​​of ChainMap are: [1, 4, 2] 

Note. Please note that a key named "b" exists in both dictionaries, but only the first key of the dictionary is accepted as the value of the key "b". Ordering is done as dictionaries are passed to functions.

Manipulating Operations

1. new_child (): — This function adds a new dictionary to the beginning of the ChainMap.

2. reversed (): — This function reverses the relative order of dictionaries in ChainMap.

Output:

 All the ChainMap contents are: [{’b’: 2,’ a’: 1}, {’b’ : 3, ’c’: 4}] Displaying new ChainMap: [{’ f’: 5}, {’b’: 2,’ a’: 1}, {’b’: 3,’ c’: 4} ] Value associated with b before reversing is: 2 Value associated with b after reversing is: 3 

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# Please select Python 3 to run this code in IDE
# Python code to demonstrate ChainMap and
# reversed () and new_child ()

 
# import collections for ChainMap operations

import collections

 
# initializing dictionaries

dic1 = { ’a’ : 1 , ’b’ : 2 }

dic2 = { ’ b’ : 3 , ’c’ : 4 }

dic3 = { ’f’ : 5 }

 
# initializing ChainMap

chain = collections.ChainMap (dic1, dic2)

 
# print a chain Map using a map

print ( "All the ChainMap contents are: " )

print (chain.maps )

 
# using new_child () to add a new dictionary

chain1 = chain.new_child (dic3)

 
# chain printing Map using map

print ( "Displaying new ChainMap:" )

print (chain1.maps)

 
# displays the value associated with b before o by reference

print ( "Value associated with b before reversing is: " , end = " ")

print (chain1 [ ’b’ ])

 
# reverse ChainMap

chain1.maps = reversed (chain1.maps)

 
# display the value associated with b after reverse

print ( "Value associated with b after reversing is:" , end = < / code> "")

print (chain1 [ ’b’ ])