Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
idle flips my dictionary
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 3,281 Sep-14-2018, 07:54 PM
Last Post: Larz60+
  Python IDLE 3.6.2 on WIn7 vs Pyhton 3 IDLE raspberry djdan_23 5 5,652 Sep-07-2017, 12:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020