DEV Community

I'm Just a Guy with a Computer
I'm Just a Guy with a Computer

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more