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


Groups > comp.lang.python > #15311

Re: Dictionary sorting

Date 2011-11-03 18:01 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Dictionary sorting
References <16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1> <j8v1h5$4mc$1@dough.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.2418.1320361310.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 11/03/11 16:36, Terry Reedy wrote:
>   >  Is there a way to not sort them and leave the order as is?
>
> CPython iterates (and prints) dict items in their arbitrary internal
> hash table order, which depends on the number and entry order of the
> items. It is a bug to depend on that arbitrary order in any way.

Does this "never trust it" hold even for two consecutive 
iterations over an unchanged dict?  I didn't see anything in the 
docs[1] to make such a claim, but at least from my experience, 
one can reliably assert

   d = {}
   randomly_populate(d)
   list1 = list(d.iterkeys())
   list2 = list(d.iterkeys())
   assert list1 == list2

I understand all bets are off if one adds/removes (or maybe 
changes the values) of the dict between iterations, but I didn't 
know if what I saw was merely an implementation detail that 
shouldn't be counted on.

-tkc

[1]
http://docs.python.org/library/stdtypes.html#mapping-types-dict


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


Thread

Dictionary sorting Scott Ware <scottdware@gmail.com> - 2011-11-03 11:46 -0700
  Re: Dictionary sorting John Gordon <gordon@panix.com> - 2011-11-03 18:57 +0000
    Re: Dictionary sorting Scott Ware <scottdware@gmail.com> - 2011-11-03 12:00 -0700
      Re: Dictionary sorting Chris Kaynor <ckaynor@zindagigames.com> - 2011-11-03 12:13 -0700
  Re: Dictionary sorting insomnia@gmail.com - 2011-11-03 13:01 -0700
  Re: Dictionary sorting Terry Reedy <tjreedy@udel.edu> - 2011-11-03 17:36 -0400
  Re: Dictionary sorting Tim Chase <python.list@tim.thechases.com> - 2011-11-03 18:01 -0500
    Re: Dictionary sorting Ben Finney <ben+python@benfinney.id.au> - 2011-11-04 11:06 +1100
      Re: Dictionary sorting Hrvoje Niksic <hniksic@xemacs.org> - 2011-11-04 11:20 +0100
        Re: Dictionary sorting Ben Finney <ben+python@benfinney.id.au> - 2011-11-04 23:19 +1100
  Re: Dictionary sorting Chris Angelico <rosuav@gmail.com> - 2011-11-04 10:59 +1100

csiph-web