Python Forum

Full Version: idle flips my dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using idle 3.4.3's interactive shell
If I put in the following code into the python interactive shell it reverses the dictionary spam and i don't know why.
spam = {'color': 'red', 'age': 42}

spam

idle will output {'age': 42, 'color': 'red'}

can someone tell me what is causing this.
Dictionaries use a hash table, and that's where the order of the dictionary comes from, not from how you entered it. Although I believe this behavior may be changing (or has it changed in 3.7?).

If you really need order in a dictionary, you can use OrderedDict in the collections module.
(Sep-20-2018, 03:35 AM)ichabod801 Wrote: [ -> ]Although I believe this behavior may be changing (or has it changed in 3.7?).
It changed in 3.6(a implementation detail and should not be relied upon).

From a python-dev message by GvR about 3.7.
GvR Wrote:Make it so. "Dict keeps insertion order" is the ruling. Thanks!
So in 3.7 is Dict order a guarantee.

Quote:I am using idle 3.4.3's interactive shell
Install a new version 3.4 starts to get old.
Python 3.6/3.7 and pip installation under Windows