display of dicts?
Chris Barker <[email protected]> Mon, 18 Dec 2017 17:08:46 -0800
| Newsgroups | gmane.comp.python.ipython.devel |
|---|---|
| Message-ID | <CALGmxEKCqXG67nBSqvBE=UjOgu5TO_8Fmtxay_=zsiH3NmU3rg@mail.gmail.com> |
--===============0084225678619460592==
Content-Type: multipart/alternative; boundary="94eb2c11a824474e500560a71ed3"
--94eb2c11a824474e500560a71ed3
Content-Type: text/plain; charset="UTF-8"
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
[email protected]
--94eb2c11a824474e500560a71ed3
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>As Guido has just declared that dicts will now offici=
ally preserve order:<br clear=3D"all"><div><br><a href=3D"https://mail.pyth=
on.org/pipermail/python-dev/2017-December/151283.html">https://mail.python.=
org/pipermail/python-dev/2017-December/151283.html</a><br><br></div><div>I =
was playing around=C2=A0 them in py3.6 ipython, and found some (to me) odd =
behavior:<br><br><span style=3D"font-family:monospace,monospace">In [1]: d =
=3D {'one':1, 'two':2, 'three':3}<br><br>In [2]: d<=
br>Out[2]: {'one': 1, 'three': 3, 'two': 2}<br><br>=
</span></div>Hmm -- order does not appear to be preserved.<br><br></div>But=
then:<span style=3D"font-family:monospace,monospace"></span><br><div><span=
style=3D"font-family:monospace,monospace"></span><div><span style=3D"font-=
family:monospace,monospace"><br>In [3]: str(d)<br>Out[3]: "{'one&#=
39;: 1, 'two': 2, 'three': 3}"<br><br>In [4]: repr(d)<=
br>Out[4]: "{'one': 1, 'two': 2, 'three': 3}&q=
uot;<br><br>In [5]: d.values()<br>Out[5]: dict_values([1, 2, 3])<br><br>In =
[6]: d.keys()<br>Out[6]: dict_keys(['one', 'two', 'thre=
e'])<br><br>In [7]: d.items()<br>Out[7]: dict_items([('one', 1)=
, ('two', 2), ('three', 3)])<br></span><br></div><div>Order=
IS preserved.<br><br></div><div>So presumably iPython is calling sorted() =
or some such when displaying a dict. <br><br></div><div>Is that that case? =
Is that documented anywhere?? I can't find it.<br><br></div><div>And wi=
th Python >=3D 3.6, dict order is preserved, so it would probably be bet=
ter to NOT mess with dict order when displaying them in iPython.<br><br></d=
iv><div>SIDE NOTE:<br></div><div><br>I had a bit of trouble finding this ma=
iling list -- google still points to the old ones on <a href=3D"http://scip=
y.org">scipy.org</a>. -- maybe we can put a note on the home page of those =
lists that they are been moved??<br><br></div><div>(I only noticed, 'ca=
use the archives of those stop last March)<br><br></div><div>-Chris<br></di=
v><div><br></div><div><br></div><div><br><br></div><div><br>-- <br><div cla=
ss=3D"gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><b=
r>Emergency Response Division<br>NOAA/NOS/OR&R =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0(206) 526-6959=C2=A0=C2=A0 voice<br>7600 Sand Point Way NE=
=C2=A0=C2=A0(206) 526-6329=C2=A0=C2=A0 fax<br>Seattle, WA =C2=A098115 =C2=
=A0 =C2=A0 =C2=A0=C2=A0(206) 526-6317=C2=A0=C2=A0 main reception<br><br><a =
href=3D"mailto:[email protected]" target=3D"_blank">[email protected]=
ov</a></div>
</div></div></div>
--94eb2c11a824474e500560a71ed3--
--===============0084225678619460592==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
IPython-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/ipython-dev
--===============0084225678619460592==--