Python Forum
Python 3.6 dict key iteration order - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python 3.6 dict key iteration order (/thread-10450.html)



Python 3.6 dict key iteration order - insearchofanswers87 - May-21-2018

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.


RE: Python 3.6 dict key iteration order - buran - May-21-2018

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?


RE: Python 3.6 dict key iteration order - snippsat - May-21-2018

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



RE: Python 3.6 dict key iteration order - insearchofanswers87 - May-21-2018

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


RE: Python 3.6 dict key iteration order - buran - May-21-2018

did you check your python version as @snippsat show you?


RE: Python 3.6 dict key iteration order - snippsat - May-21-2018

(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 .


RE: Python 3.6 dict key iteration order - insearchofanswers87 - May-22-2018

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.


RE: Python 3.6 dict key iteration order - snippsat - May-22-2018

(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