[IPython-dev] display of dicts?

Chris Barker chris.barker at noaa.gov
Mon Dec 18 20:08:46 EST 2017


As Guido has just declared that dicts will now officially preserve order:

https://mail.python.org/pipermail/python-dev/2017-December/151283.html

I was playing around  them in py3.6 ipython, and found some (to me) odd
behavior:

In [1]: d = {'one':1, 'two':2, 'three':3}

In [2]: d
Out[2]: {'one': 1, 'three': 3, 'two': 2}

Hmm -- order does not appear to be preserved.

But then:

In [3]: str(d)
Out[3]: "{'one': 1, 'two': 2, 'three': 3}"

In [4]: repr(d)
Out[4]: "{'one': 1, 'two': 2, 'three': 3}"

In [5]: d.values()
Out[5]: dict_values([1, 2, 3])

In [6]: d.keys()
Out[6]: dict_keys(['one', 'two', 'three'])

In [7]: d.items()
Out[7]: dict_items([('one', 1), ('two', 2), ('three', 3)])

Order IS preserved.

So presumably iPython is calling sorted() or some such when displaying a
dict.

Is that that case? Is that documented anywhere?? I can't find it.

And with Python >= 3.6, dict order is preserved, so it would probably be
better to NOT mess with dict order when displaying them in iPython.

SIDE NOTE:

I had a bit of trouble finding this mailing list -- google still points to
the old ones on scipy.org. -- maybe we can put a note on the home page of
those lists that they are been moved??

(I only noticed, 'cause the archives of those stop last March)

-Chris





-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20171218/63719b6f/attachment.html>


More information about the IPython-dev mailing list