[IPython-dev] display of dicts?

Nathan Goldbaum nathan12343 at gmail.com
Mon Dec 18 20:40:02 EST 2017


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 <chris.barker at noaa.gov> wrote:

> 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
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at python.org
> https://mail.python.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20171218/7acbcb66/attachment.html>


More information about the IPython-dev mailing list