Re: display of dicts?

Chris Barker <[email protected]> Tue, 19 Dec 2017 09:24:24 -0800
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CALGmxE+BApDuX8EWX1XqVNShXWVx91MxyVAJb8QeW1kJWh6OWg@mail.gmail.com>
--===============9068840360529186133==
Content-Type: multipart/alternative; boundary="089e082e96206ee1e90560b4bf6d"

--089e082e96206ee1e90560b4bf6d
Content-Type: text/plain; charset="UTF-8"

On Tue, Dec 19, 2017 at 7:45 AM, Thomas Kluyver <[email protected]> wrote:

> I think there's less danger of us breaking someone's *code* that relies on
> dictionary presentation. But it's more important for us to think about
> what's useful for human interpretation, since IPython is all about the
> interface.
>

exactly.


> I disagree with that. Consistency is part of it, but I think the sorting
>> is often helpful in itself. It's much easier to see if the dictionary has a
>> key 'foo' if it's shown in alphabetical order than if it's not.
>>
>
> The new dict also only gives you consistency if the data going into it is
> consistently ordered. If you are building a dictionary of filenames with
> os.listdir(), for example, order is not guaranteed.
>

but whether default sort order of the keys is meaningful is a happy
coincidence -- if the keys happen to be strings for which alphabetization
makes sense, then great. If they are anything else, then not so great.

Granted, string keys where alphabetization makes sense are pretty darn
common, but still a "special case"

In fact, I discovered this with this example, which I used in my intro to
Python class to demonstrate dict's arbitrary order:

In python 2.7:


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


In [*2*]: d

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


In [*3*]: d.keys()

Out[*3*]: ['three', 'two', 'one']

Interesting that the display version is different than the d.keys() version
-- but I just thought that was a nice demo of "dicts are arbitrary order"

Then, in Python3.6, which I am now using for teaching:

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


In [*2*]: d

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


In [*3*]: d.keys()

Out[*3*]: dict_keys(['one', 'two', 'three'])


In [*4*]: print(d)

{'one': 1, 'two': 2, 'three': 3}

Huh? d.keys() is preserving order, print is preserving order, but plain
display is not. it took me some time to realize that the display order was
alphabetical, and then I figured that iPython must be sorting them, which
then led me to the fact that the stdlib pprint sorts them too.

I have been suing iPython for many years, and had never noticed that
display used something other than "str" or "repr", nor that pprint sorted
dicts.

Still not sure if we should change it, but I don't think sorting dicts
based on default sort of keys is an obviously "best" way to present a dict.

-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]

--089e082e96206ee1e90560b4bf6d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On T=
ue, Dec 19, 2017 at 7:45 AM, Thomas Kluyver <span dir=3D"ltr">&lt;<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt;</sp=
an> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px=
 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D=
"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div>I think th=
ere&#39;s less danger of us breaking someone&#39;s *code* that relies on di=
ctionary presentation. But it&#39;s more important for us to think about wh=
at&#39;s useful for human interpretation, since IPython is all about the in=
terface.</div></div></div></div></blockquote><div><br></div><div>exactly.</=
div><div>=C2=A0<br></div><blockquote class=3D"gmail_quote" style=3D"margin:=
0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">=
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote"><spa=
n><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;borde=
r-left:1px solid rgb(204,204,204);padding-left:1ex">I disagree with that. C=
onsistency is part of it, but I think the sorting is often helpful in itsel=
f. It&#39;s much easier to see if the dictionary has a key &#39;foo&#39; if=
 it&#39;s shown in alphabetical order than if it&#39;s not.<br></blockquote=
></span></div><div class=3D"gmail_quote"><br></div><div class=3D"gmail_quot=
e">The new dict also only gives you consistency if the data going into it i=
s consistently ordered. If you are building a dictionary of filenames with =
os.listdir(), for example, order is not guaranteed.</div></div></div></bloc=
kquote><div><br></div><div>but whether default sort order of the keys is me=
aningful is a happy coincidence -- if the keys happen to be strings for whi=
ch alphabetization makes sense, then great. If they are anything else, then=
 not so great.</div><div><br></div><div>Granted, string keys where alphabet=
ization makes sense are pretty darn common, but still a &quot;special case&=
quot;</div><div><br></div><div>In fact, I discovered this with this example=
, which I used in my intro to Python class to demonstrate dict&#39;s arbitr=
ary order:</div><div><br></div><div>In python 2.7:</div><div>







<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(205,121,35);background-color:rgb(255,255,255=
)"><span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligat=
ures;color:rgb(52,163,39)"><br></span></p><p class=3D"gmail-p1" style=3D"ma=
rgin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stre=
tch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(20=
5,121,35);background-color:rgb(255,255,255)"><span class=3D"gmail-s1" style=
=3D"font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">In [</=
span><span class=3D"gmail-s2" style=3D"font-variant-ligatures:no-common-lig=
atures;color:rgb(46,231,33)"><b>1</b></span><span class=3D"gmail-s1" style=
=3D"font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">]: </s=
pan><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-liga=
tures;color:rgb(0,0,0)">d =3D {</span><span class=3D"gmail-s4" style=3D"fon=
t-variant-ligatures:no-common-ligatures">&quot;one&quot;</span><span class=
=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb=
(0,0,0)">: </span><span class=3D"gmail-s1" style=3D"font-variant-ligatures:=
no-common-ligatures;color:rgb(52,163,39)">1</span><span class=3D"gmail-s3" =
style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, </s=
pan><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-liga=
tures">&quot;two&quot;</span><span class=3D"gmail-s3" style=3D"font-variant=
-ligatures:no-common-ligatures;color:rgb(0,0,0)">: </span><span class=3D"gm=
ail-s1" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(52,16=
3,39)">2</span><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-=
common-ligatures;color:rgb(0,0,0)">, </span><span class=3D"gmail-s4" style=
=3D"font-variant-ligatures:no-common-ligatures">&quot;three&quot;</span><sp=
an class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(0,0,0)">: </span><span class=3D"gmail-s1" style=3D"font-variant-li=
gatures:no-common-ligatures;color:rgb(52,163,39)">3</span><span class=3D"gm=
ail-s3" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0=
)">}</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min=
-height:13px"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-c=
ommon-ligatures"></span><br></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)=
"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligatu=
res">In [</span><span class=3D"gmail-s2" style=3D"font-variant-ligatures:no=
-common-ligatures;color:rgb(46,231,33)"><b>2</b></span><span class=3D"gmail=
-s4" style=3D"font-variant-ligatures:no-common-ligatures">]: </span><span c=
lass=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;color=
:rgb(0,0,0)">d</span></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(178,54,34)">Out[</span><span class=3D"gmail-s6" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>2</b></span><sp=
an class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(178,54,34)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">{&#39;one&#39;: 1, &#39;three&#39;: 3, &#=
39;two&#39;: 2}</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min=
-height:13px"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-c=
ommon-ligatures"></span><br></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(52,163,39)">In [</span><span class=3D"gmail-s2" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(46,231,33)"><b>3</b></span><sp=
an class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(52,163,39)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">d.keys()</span></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(178,54,34)">Out[</span><span class=3D"gmail-s6" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>3</b></span><sp=
an class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(178,54,34)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">[&#39;three&#39;, &#39;two&#39;, &#39;one=
&#39;]</span></p></div><div><br></div><div>Interesting that the display ver=
sion is different than the d.keys() version -- but I just thought that was =
a nice demo of &quot;dicts are arbitrary order&quot;</div><div><br></div><d=
iv>Then, in Python3.6, which I am now using for teaching:</div><div><br></d=
iv><div>







<p class=3D"gmail-p1" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(205,121,35);background-color:rgb(255,255,255=
)"><span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligat=
ures;color:rgb(52,163,39)">In [</span><span class=3D"gmail-s2" style=3D"fon=
t-variant-ligatures:no-common-ligatures;color:rgb(46,231,33)"><b>1</b></spa=
n><span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatu=
res;color:rgb(52,163,39)">]: </span><span class=3D"gmail-s3" style=3D"font-=
variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">d =3D {</span><span=
 class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligatures">&q=
uot;one&quot;</span><span class=3D"gmail-s3" style=3D"font-variant-ligature=
s:no-common-ligatures;color:rgb(0,0,0)">: </span><span class=3D"gmail-s1" s=
tyle=3D"font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">1<=
/span><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-li=
gatures;color:rgb(0,0,0)">, </span><span class=3D"gmail-s4" style=3D"font-v=
ariant-ligatures:no-common-ligatures">&quot;two&quot;</span><span class=3D"=
gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0=
,0)">: </span><span class=3D"gmail-s1" style=3D"font-variant-ligatures:no-c=
ommon-ligatures;color:rgb(52,163,39)">2</span><span class=3D"gmail-s3" styl=
e=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, </span>=
<span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligature=
s">&quot;three&quot;</span><span class=3D"gmail-s3" style=3D"font-variant-l=
igatures:no-common-ligatures;color:rgb(0,0,0)">: </span><span class=3D"gmai=
l-s1" style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(52,163,=
39)">3</span><span class=3D"gmail-s3" style=3D"font-variant-ligatures:no-co=
mmon-ligatures;color:rgb(0,0,0)">}</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min=
-height:13px"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-c=
ommon-ligatures"></span><br></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)=
"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligatu=
res">In [</span><span class=3D"gmail-s2" style=3D"font-variant-ligatures:no=
-common-ligatures;color:rgb(46,231,33)"><b>2</b></span><span class=3D"gmail=
-s4" style=3D"font-variant-ligatures:no-common-ligatures">]: </span><span c=
lass=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;color=
:rgb(0,0,0)">d</span></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(178,54,34)">Out[</span><span class=3D"gmail-s6" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>2</b></span><sp=
an class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(178,54,34)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">{&#39;one&#39;: 1, &#39;three&#39;: 3, &#=
39;two&#39;: 2}</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min=
-height:13px"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-c=
ommon-ligatures"></span><br></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(52,163,39)">In [</span><span class=3D"gmail-s2" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(46,231,33)"><b>3</b></span><sp=
an class=3D"gmail-s1" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(52,163,39)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">d.keys()</span></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(178,54,34)">Out[</span><span class=3D"gmail-s6" style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>3</b></span><sp=
an class=3D"gmail-s5" style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(178,54,34)">]: </span><span class=3D"gmail-s4" style=3D"font-varia=
nt-ligatures:no-common-ligatures">dict_keys([&#39;one&#39;, &#39;two&#39;, =
&#39;three&#39;])</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min=
-height:13px"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-c=
ommon-ligatures"></span><br></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)=
"><span class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligatu=
res">In [</span><span class=3D"gmail-s2" style=3D"font-variant-ligatures:no=
-common-ligatures;color:rgb(46,231,33)"><b>4</b></span><span class=3D"gmail=
-s4" style=3D"font-variant-ligatures:no-common-ligatures">]: print</span><s=
pan class=3D"gmail-s3" style=3D"font-variant-ligatures:no-common-ligatures;=
color:rgb(0,0,0)">(d)</span></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-style:normal;font-variant:no=
rmal;font-weight:normal;font-stretch:normal;font-size:11px;line-height:norm=
al;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><s=
pan class=3D"gmail-s4" style=3D"font-variant-ligatures:no-common-ligatures"=
>{&#39;one&#39;: 1, &#39;two&#39;: 2, &#39;three&#39;: 3}</span></p></div><=
div><br></div><div>Huh? d.keys() is preserving order, print is preserving o=
rder, but plain display is not. it took me some time to realize that the di=
splay order was alphabetical, and then I figured that iPython must be sorti=
ng them, which then led me to the fact that the stdlib pprint sorts them to=
o.</div><div><br></div><div>I have been suing iPython for many years, and h=
ad never noticed that display used something other than &quot;str&quot; or =
&quot;repr&quot;, nor that pprint sorted dicts.</div><div><br></div><div>St=
ill not sure if we should change it, but I don&#39;t think sorting dicts ba=
sed on default sort of keys is an obviously &quot;best&quot; way to present=
 a dict.</div><div><br></div><div>-Chris</div></div><div class=3D"gmail_ext=
ra"><br></div><div class=3D"gmail_extra"><br></div>-- <br><div class=3D"gma=
il-m_-3347348982731435322gmail_signature"><br>Christopher Barker, Ph.D.<br>=
Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&amp;R =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"tel:(206)%20526-6959" valu=
e=3D"+12065266959" target=3D"_blank">(206) 526-6959</a>=C2=A0=C2=A0 voice<b=
r>7600 Sand Point Way NE =C2=A0=C2=A0<a href=3D"tel:(206)%20526-6329" value=
=3D"+12065266329" target=3D"_blank">(206) 526-6329</a>=C2=A0=C2=A0 fax<br>S=
eattle, WA =C2=A098115 =C2=A0 =C2=A0 =C2=A0=C2=A0<a href=3D"tel:(206)%20526=
-6317" value=3D"+12065266317" target=3D"_blank">(206) 526-6317</a>=C2=A0=C2=
=A0 main reception<br><br><a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a></div>
</div></div>

--089e082e96206ee1e90560b4bf6d--

--===============9068840360529186133==
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

--===============9068840360529186133==--