DEV Community

Mr #1738
Mr #1738

Posted on

Updating Map in Python

when the element of the dictionary is updated, the result is instantly updated in the result of the ChainMap, Enter the below code and try to execute it to see how updating Maps is done.

import collections

dict1 = {"player1":'cole palmer',"player2":'Raheem Sterling',"player3":'Reece james'}
dict2  = {"player4":'Levi Colwil',"player5":'Moises Caicedo',"player6":'Conor Callagher'}

res  = collections.ChainMap(dict1,dict2)
print(res.maps, '\n')


dict2['player4'] = 'Noni Madueke'
print(res.maps, '\n')
Enter fullscreen mode Exit fullscreen mode

output
When the above code is executed, it produces the following result .

[{'player1': 'cole palmer', 'player2': 'Raheem Sterling', 'player3': 'Reece james'}, {'player4': 'Levi Colwil', 'player5': 'Moises Caicedo', 'player6': 'Conor Callagher'}] 

[{'player1': 'cole palmer', 'player2': 'Raheem Sterling', 'player3': 'Reece james'}, {'player4': 'Noni Madueke', 'player5': 'Moises Caicedo', 'player6': 'Conor Callagher'}]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)