Python Forum
Python 3.6 dict key iteration order
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.6 dict key iteration order
#1
According to the Python 3.6 What's New docs, dict now maintains keys in sorted order. Given that, why does displaying the string representation of a dict show the keys in sorted order, but iterating through the dictionary's items() or keys() returns them in insertion order?

In [10]: d = {}

In [11]: d['orange'] = 1

In [12]: d['blue'] = 2

In [13]: d['green'] = 3

In [14]: d
Out[14]: {'blue': 2, 'green': 3, 'orange': 1}

In [15]: for i in d.items():
    ...:     print(i)
    ...:     
('orange', 1)
('blue', 2)
('green', 3)
It seems to me that to create the string representation requires iterating through the keys and they should produce the same order.
Reply
#2
Note that it's not sorted, but order-preserving. These are two different things. Also it is considered implementation details and should not be relied upon. So the iteration is "correct".
Quote:The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).

All that said are you certain you are using 3.6?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
As mention check that you use 3.6.here also with f-string.
So now will you get syntax error if not use 3.6.
You see that order is preserved in my code.
# Check python version(this is done from command line)
E:\1py_div
λ python -V
Python 3.6.4

# Check pip
E:\1py_div
λ pip -V
pip 10.0.1 from c:\python36\lib\site-packages\pip (python 3.6)

E:\1py_div
λ ptpython
>>> d = {}
>>> d['orange'] = 1
>>> d['blue'] = 2
>>> d['green'] = 3
>>> d
{'orange': 1, 'blue': 2, 'green': 3}

>>> for key,value in d.items():
...     print(f'{key} --> {value*value}')
orange --> 1
blue --> 4
green --> 9
Reply
#4
Yes, sorry, order preserving. So, why is the string representation not order preserving, but rather displays keys in sorted order. Again, to produce that string, they need to iterate over the dictionary, so that, too, should preserve order.

I realize we should not depend on the order, but based on the notes, it appears that they're eventually planning to make that order preserving required and it should be consistent across all ways of iterating--even if they do the iterating for you in the case of creating a string representation
Reply
#5
did you check your python version as @snippsat show you?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-21-2018, 08:34 PM)insearchofanswers87 Wrote: I realize we should not depend on the order,
No problem to depend on order,in 3.7 the order will be guaranteed as a rule.
Mail form Guido van Rossum to developer Raymond Hettinger about 3.7:
GVR Wrote:Make it so. "Dict keeps insertion order" is the ruling. Thanks!
If you use 3.6 the code should look as mine where order is preserved,not like yours so we guess that you are not using 3,6 .
Reply
#7
I am using 3.6.4.
My main question is still open--why is the dict's string representation not the same order as iteration with a "for" statement? They both iterate, so they both should produce the same ordering.
Reply
#8
(May-22-2018, 05:05 PM)insearchofanswers87 Wrote: I am using 3.6.4.
Upgrade IPython,that's what messing it up.
pip install -U ipython
Before upgrade:
G:\code\planes
λ ipython
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: d = {}

In [2]: d['orange'] = 1

In [3]: d['blue'] = 2

In [4]: d['green'] = 3

In [5]: d
Out[5]: {'blue': 2, 'green': 3, 'orange': 1}
After upgrade,now version 6.4.0 and the dictionary order are as it should with 3.6:
G:\code\planes
λ ipython
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [2]: d = {}

In [3]: d['orange'] = 1

In [4]: d['blue'] = 2

In [5]: d['green'] = 3

In [6]: d
Out[6]: {'orange': 1, 'blue': 2, 'green': 3}

In [9]: for key,value in d.items():
   ...:     print(f'{key} --> {value}')
   ...:
orange --> 1
blue --> 2
green --> 3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understand order of magnitude performance gap between python and C++ ThelannOryat 4 2,700 Mar-17-2021, 03:39 PM
Last Post: ThelannOryat
  String index out of bounds ( Python : Dict ) kommu 2 2,386 Jun-25-2020, 08:52 PM
Last Post: menator01
  Python 2 to 3 dict sorting joshuaprocious 2 57,347 May-14-2020, 03:28 PM
Last Post: joshuaprocious
  Sort a dict in dict cherry_cherry 4 72,942 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Python list - group by dict key karthidec 2 9,404 Nov-25-2019, 06:58 AM
Last Post: buran
  Python Turtle and order of implementation query Parsleigh 2 2,756 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  python result problem of an iteration algorithm for power allocation Jessica 1 2,622 Sep-07-2018, 08:08 PM
Last Post: micseydel
  Python Iteration Understanding giteepag 3 2,701 Jul-26-2018, 02:23 PM
Last Post: perfringo
  Python 2.7 Addition to dict is too slow VolanD 6 4,027 May-04-2018, 09:24 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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