The Order Of Dictionary Entries

56 views
Skip to first unread message

Tim Daneliuk

unread,
Mar 10, 2004, 6:11:26 AM3/10/04
to
I am aware that dictionary order is not guaranteed. But I ran into
something puzzling today. I filled a dictionary dynamically as a
program ran. Up to a certain point, the key order was the order
in which things were added. However, at some point, the dictionary
appeared to reorder its contents in some non-deterministic way - in
fact, it looked like it was putting all the uppercase keys first,
followed by lowercase keys.

I'm just curious, just what _is_ the approach used for ordering
dictionary keys?

(This was confirmed on both Unix and Win32, BTW).
--
----------------------------------------------------------------------------
Tim Daneliuk tun...@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

Aahz

unread,
Mar 10, 2004, 6:57:58 AM3/10/04
to
In article <in93i1-...@eskimo.tundraware.com>,

Tim Daneliuk <tun...@tundraware.com> wrote:
>
>I am aware that dictionary order is not guaranteed. But I ran into
>something puzzling today. I filled a dictionary dynamically as a
>program ran. Up to a certain point, the key order was the order
>in which things were added. However, at some point, the dictionary
>appeared to reorder its contents in some non-deterministic way - in
>fact, it looked like it was putting all the uppercase keys first,
>followed by lowercase keys.
>
>I'm just curious, just what _is_ the approach used for ordering
>dictionary keys?

Use the source, Luke. See Objects/dictobject.c

Hint: dicts are based on hashing.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python

Terry Reedy

unread,
Mar 10, 2004, 12:28:01 PM3/10/04
to pytho...@python.org

"Tim Daneliuk" <tun...@tundraware.com> wrote in message
news:in93i1-...@eskimo.tundraware.com...

> I am aware that dictionary order is not guaranteed. But I ran into
> something puzzling today. I filled a dictionary dynamically as a
> program ran. Up to a certain point, the key order was the order
> in which things were added.

I am rather certain that this was an accident dependent on you adding the
keys in the order in which they were going to displayed. Try adding the
items in a different order and the display order should not change much if
at all. Example:

>>> {1:1, 2:2}
{1: 1, 2: 2}
>>> {2:2, 1:1}
{1: 1, 2: 2}

Terry J. Reedy


Reply all
Reply to author
Forward
0 new messages