DEV Community

Mr #1738
Mr #1738

Posted on

Map Reordering in Python

if we change the order of dictionaries while clubbing them in below example we see the positionof elements get interchanged as if they are in continuos chain.
Enter the following code and try to excute it you see the outcome.

import collections

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



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


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

Enter fullscreen mode Exit fullscreen mode

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'}] 

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

Enter fullscreen mode Exit fullscreen mode

Top comments (0)