display of dicts?

Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

display of dicts?

Chris Barker
As Guido has just declared that dicts will now officially preserve order:
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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Nathan Goldbaum
IPython does use pretty-printing by default. You can control it with the %pprint magic, in your IPython configuration, with the PlainTextFormatter.pprint option, or with the --pprint command line argument when starting IPython.

http://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-PlainTextFormatter.pprint



On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[hidden email]> wrote:
As Guido has just declared that dicts will now officially preserve order:
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            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker
Thanks Nathan,

And to confirm, this is the stdlib's pprint module, yes?

(which does seem to show the same behavior)

-CHB



On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <[hidden email]> wrote:
IPython does use pretty-printing by default. You can control it with the %pprint magic, in your IPython configuration, with the PlainTextFormatter.pprint option, or with the --pprint command line argument when starting IPython.

http://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-PlainTextFormatter.pprint



On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[hidden email]> wrote:
As Guido has just declared that dicts will now officially preserve order:
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            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Nathan Goldbaum

On Mon, Dec 18, 2017 at 7:56 PM, Chris Barker <[hidden email]> wrote:
Thanks Nathan,

And to confirm, this is the stdlib's pprint module, yes?

(which does seem to show the same behavior)

-CHB



On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <[hidden email]> wrote:
IPython does use pretty-printing by default. You can control it with the %pprint magic, in your IPython configuration, with the PlainTextFormatter.pprint option, or with the --pprint command line argument when starting IPython.

http://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-PlainTextFormatter.pprint



On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[hidden email]> wrote:
As Guido has just declared that dicts will now officially preserve order:
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            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker


On Mon, Dec 18, 2017 at 7:04 PM, Nathan Goldbaum <[hidden email]> wrote:

I see, thanks! Though it does have the same behavior of sorting dicts -- you can see that in:

``_dict_pprinter_factory``

However as of cPYthon 3.6, and officially as of 3.7, dicts will maintain their insertion order.

So we should probably remove the sorting from the _dict_pprinter_factory.

-CHB


 


On Mon, Dec 18, 2017 at 7:56 PM, Chris Barker <[hidden email]> wrote:
Thanks Nathan,

And to confirm, this is the stdlib's pprint module, yes?

(which does seem to show the same behavior)

-CHB



On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <[hidden email]> wrote:
IPython does use pretty-printing by default. You can control it with the %pprint magic, in your IPython configuration, with the PlainTextFormatter.pprint option, or with the --pprint command line argument when starting IPython.

http://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-PlainTextFormatter.pprint



On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[hidden email]> wrote:
As Guido has just declared that dicts will now officially preserve order:
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            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Thomas Kluyver-2
I think this is a tricky one, as the discussion on Python-dev is finding. Removing the sorting makes it easier to explain how dictionaries now work. But for a lot of real-world use cases, insertion order is not meaningful, and displaying a sorted dictionary is still going to be more useful.

We'll probably have to make it optional, but I don't think it's at all obvious which should be the default. I think the sorting is probably preferable more of the time, but when trying to teach people about insertion order, you really don't want to have to switch to non-default behaviour.

On 19 December 2017 at 04:19, Chris Barker <[hidden email]> wrote:


On Mon, Dec 18, 2017 at 7:04 PM, Nathan Goldbaum <[hidden email]> wrote:

I see, thanks! Though it does have the same behavior of sorting dicts -- you can see that in:

``_dict_pprinter_factory``

However as of cPYthon 3.6, and officially as of 3.7, dicts will maintain their insertion order.

So we should probably remove the sorting from the _dict_pprinter_factory.

-CHB


 


On Mon, Dec 18, 2017 at 7:56 PM, Chris Barker <[hidden email]> wrote:
Thanks Nathan,

And to confirm, this is the stdlib's pprint module, yes?

(which does seem to show the same behavior)

-CHB



On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <[hidden email]> wrote:
IPython does use pretty-printing by default. You can control it with the %pprint magic, in your IPython configuration, with the PlainTextFormatter.pprint option, or with the --pprint command line argument when starting IPython.

http://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-PlainTextFormatter.pprint



On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[hidden email]> wrote:
As Guido has just declared that dicts will now officially preserve order:
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            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev




--

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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker
> On Dec 19, 2017, at 3:23 AM, Thomas Kluyver <[hidden email]> wrote:
>
> I think this is a tricky one, as the discussion on Python-dev is finding.

Indeed. But I think with iPython, at least it’s less likely that
people are using the pretty printed results in a meaningful way.

> But for a lot of real-world use cases, insertion order is not meaningful, and displaying a sorted dictionary is still going to be more useful.

Maybe — but for many dicts, the default sort order isn’t any better
than arbitrary anyway. In my mind, the reason to have sorted dicts for
pretty print in the first place was to get consistency— not sorting
per se. And we now get that out of the box.

> We'll probably have to make it optional, but I don't think it's at all obvious which should be the default. I think the sorting is probably preferable more of the time, but when trying to teach people about insertion order, you really don't want to have to switch to non-default behaviour.

Exactly.

+1 for making sorting non-default.

But it is a change .. maybe not worth it.

-CHB
_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Thomas Kluyver-2
On 19 December 2017 at 15:37, Chris Barker - NOAA Federal <[hidden email]> wrote:
> On Dec 19, 2017, at 3:23 AM, Thomas Kluyver <[hidden email]> wrote:
>
> I think this is a tricky one, as the discussion on Python-dev is finding.

Indeed. But I think with iPython, at least it’s less likely that
people are using the pretty printed results in a meaningful way.

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.
 
> But for a lot of real-world use cases, insertion order is not meaningful, and displaying a sorted dictionary is still going to be more useful.

Maybe — but for many dicts, the default sort order isn’t any better
than arbitrary anyway. In my mind, the reason to have sorted dicts for
pretty print in the first place was to get consistency— not sorting
per se. And we now get that out of the box.

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.

Thomas

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker
On Tue, Dec 19, 2017 at 7:45 AM, Thomas Kluyver <[hidden email]> 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            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Thomas Kluyver-2
On 19 December 2017 at 17:24, Chris Barker <[hidden email]> wrote:
Granted, string keys where alphabetization makes sense are pretty darn common, but still a "special case"

I'd argue that they're common enough to be the typical case, though perhaps my definition of 'makes sense' is a bit broader than yours. And sorting can also make sense for numeric keys.
 
In fact, I discovered this with this example, which I used in my intro to Python class to demonstrate dict's arbitrary order

I think this is exactly the tension we're facing. People teaching intro to Python courses want to show what a dictionary *is*, and you don't want IPython's features to obscure that. But other than learning about dictionaries, I suspect that sorted order is useful more often than insertion order.

I'm leaning slightly towards making insertion order the default in Python 3.7 - it will be meaningful some of the time, and it is now part of what a dict is - but I think we should ensure the same release adds an easy way to switch back to displaying sorted dicts.

Thomas


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Steve Holden-5
On Tue, Dec 19, 2017 at 5:37 PM, Thomas Kluyver <[hidden email]> wrote:
On 19 December 2017 at 17:24, Chris Barker <[hidden email]> wrote:
Granted, string keys where alphabetization makes sense are pretty darn common, but still a "special case"

I'd argue that they're common enough to be the typical case, though perhaps my definition of 'makes sense' is a bit broader than yours. And sorting can also make sense for numeric keys.
 
In fact, I discovered this with this example, which I used in my intro to Python class to demonstrate dict's arbitrary order

I think this is exactly the tension we're facing. People teaching intro to Python courses want to show what a dictionary *is*, and you don't want IPython's features to obscure that. But other than learning about dictionaries, I suspect that sorted order is useful more often than insertion order.

I'm leaning slightly towards making insertion order the default in Python 3.7 - it will be meaningful some of the time, and it is now part of what a dict is - but I think we should ensure the same release adds an easy way to switch back to displaying sorted dicts.

​What about adding a "sorted" method to dicts that produces a dict with the same keys but the keys inserted ​in sorted order?

regards
 Steve

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker
In reply to this post by Thomas Kluyver-2
On Tue, Dec 19, 2017 at 9:37 AM, Thomas Kluyver <[hidden email]> wrote:
I'm leaning slightly towards making insertion order the default in Python 3.7 - it will be meaningful some of the time, and it is now part of what a dict is - but I think we should ensure the same release adds an easy way to switch back to displaying sorted dicts.

Sounds like a good plan. while we're at it -- could we add a way to specify a sort key function?

-CHB
 

--

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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Chris Barker
In reply to this post by Steve Holden-5
On Tue, Dec 19, 2017 at 9:50 AM, Steve Holden <[hidden email]> wrote:

​What about adding a "sorted" method to dicts that produces a dict with the same keys but the keys inserted ​in sorted order?

That would be a job for python-dev, yes?

And maybe .sort() that sorts in place instead -- similar to lists.

Too bad that the plain sorted() function only gives you the keys.. though there is 

dict(sorted(d.items()))


to get a sorted dict. 


-CHB


--

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

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: display of dicts?

Steve Holden-5
I've just "run it up the flagpole" on python-dev, so we'll see if anyone salutes.

Steve Holden

On Tue, Dec 19, 2017 at 5:55 PM, Chris Barker <[hidden email]> wrote:
On Tue, Dec 19, 2017 at 9:50 AM, Steve Holden <[hidden email]> wrote:

​What about adding a "sorted" method to dicts that produces a dict with the same keys but the keys inserted ​in sorted order?

That would be a job for python-dev, yes?

And maybe .sort() that sorts in place instead -- similar to lists.

Too bad that the plain sorted() function only gives you the keys.. though there is 

dict(sorted(d.items()))


to get a sorted dict. 


-CHB


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            <a href="tel:(206)%20526-6959" value="+12065266959" target="_blank">(206) 526-6959   voice
7600 Sand Point Way NE   <a href="tel:(206)%20526-6329" value="+12065266329" target="_blank">(206) 526-6329   fax
Seattle, WA  98115       <a href="tel:(206)%20526-6317" value="+12065266317" target="_blank">(206) 526-6317   main reception

[hidden email]

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev