Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have a small function in python. When i pass a dictionary(named 'hand') to the function and return it, the contents of the dictionary get swapped.

def updateHand(hand, word):
return hand

print updateHand({'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1},"quail")

OUTPUT:
{'a': 1, 'i': 1, 'm': 1, 'l': 2, 'q': 1, 'u': 1} - Dictionary contents swapped

You can see the difference in the output.
Has any one experienced this before?
How can i avoid mutation of the dictionary contents while passing to a function.

Thanks,
Rahul
Posted
Updated 2-Feb-15 18:58pm
v2

1 solution

copy() is probably using the insert order into the dictionary and not sorting the "keys" first. Dictionarys seldom guarantee sorting of the keys.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900