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


Groups > comp.lang.python > #41401

Re: Just curious - I thought dict keys would maintain sequence

References <ki6fig$n7t$1@ger.gmane.org>
Date 2013-03-18 18:31 +1100
Subject Re: Just curious - I thought dict keys would maintain sequence
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3414.1363591901.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Mar 18, 2013 at 6:26 PM, Frank Millman <frank@chagford.com> wrote:
> Hi all
>
> I know that you cannot rely on the order of keys in a dictionary, and I am
> not attempting to do so.
>
> Nevertheless, the following surprised me. A program creates a dictionary
> with a known set of keys. I would have thought that multiple runs of the
> program would return the keys in the same sequence. As you can see, the
> results appear to be totally random.
>
> Just out of interest, I would appreciate an explanation.

Mainly, it's just something you utterly cannot depend on, so it's
allowed to vary based on the phase of the moon, the position of your
hard drive platters, or anything else it likes. The hashing is
actually randomized in recent versions of Python as a defense against
a denial of service attack by generating hash collisions; that's most
likely what you're seeing there. The dictionary object is not
deterministic to that extent. :)

ChrisA

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


Thread

Re: Just curious - I thought dict keys would maintain sequence Chris Angelico <rosuav@gmail.com> - 2013-03-18 18:31 +1100
  Re: Just curious - I thought dict keys would maintain sequence Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 07:59 +0000

csiph-web