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

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Ordering in the printout of a dictionary
Date 2014-03-18 02:56 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <lg8ckl$aro$1@reader1.panix.com> (permalink)
References <lg8462$fnk$1@news.albasani.net> <mailman.8231.1395103693.18130.python-list@python.org>

Show all headers | 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