Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68476

Re: Ordering in the printout of a dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.linkpendium.com!news.linkpendium.com!panix!gordon
From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Ordering in the printout of a dictionary
Date Tue, 18 Mar 2014 02:56:21 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 19
Message-ID <lg8ckl$aro$1@reader1.panix.com> (permalink)
References <lg8462$fnk$1@news.albasani.net> <mailman.8231.1395103693.18130.python-list@python.org>
NNTP-Posting-Host panix3.panix.com
X-Trace reader1.panix.com 1395111381 11128 166.84.1.3 (18 Mar 2014 02:56:21 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Tue, 18 Mar 2014 02:56:21 +0000 (UTC)
User-Agent nn/6.7.3
Xref csiph.com comp.lang.python:68476

Show key headers only | View raw


In <mailman.8231.1395103693.18130.python-list@python.org> Chris Angelico <rosuav@gmail.com> writes:

> > Is there a way to force a certain ordering of the printout or else
> > somehow manage to get at least a certain stable ordering of the
> > printout (i.e. input and output are identical)?

> Yes; instead of simply printing it out (which calls repr()),
> explicitly iterate over it, like this:

> def display(d):
>     return '{'+','.join('%r: %r'%(key,d[key]) for key in sorted(d))+'}'

You could also use the OrderedDict type, which is subclass of dict that
preserves insertion order.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Ordering in the printout of a dictionary Mok-Kong Shen <mok-kong.shen@t-online.de> - 2014-03-18 01:32 +0100
  Re: Ordering in the printout of a dictionary Chris Angelico <rosuav@gmail.com> - 2014-03-18 11:48 +1100
    Re: Ordering in the printout of a dictionary John Gordon <gordon@panix.com> - 2014-03-18 02:56 +0000
    Re: Ordering in the printout of a dictionary Marc Christiansen <usenetmail@solar-empire.de> - 2014-03-18 08:36 +0100
      Re: Ordering in the printout of a dictionary Chris Angelico <rosuav@gmail.com> - 2014-03-18 18:55 +1100

csiph-web