can the sequence of entries in a dictionary be depended on?

Rhamphoryncus rhamph at
Mon Nov 24 09:35:52 CET 2008

On Nov 23, 6:43 pm, John Machin <sjmac... at> wrote:
> On Nov 24, 11:59 am, Carsten Haese <carsten.ha... at> wrote:
> > Diez B. Roggisch wrote:
> > > AFAIK the order is deterministic as long as you don't alter the dict between
> > > iterations. However, this is an implementation detail.
> > It's not an implementation detail. It's documented behavior. Thus quoth
> > """
> > If items(), keys(), values(), iteritems(), iterkeys(), and itervalues()
> > are called with no intervening modifications to the dictionary, the
> > lists will directly correspond.
> > """
> Changing the value attached to an existing key is a "modification to
> the dictionary", but it's hard to see how that would change the
> iteration order.

Although the referenced docs don't clarify it, changing the value
without adding or removing a key (even temporarily) does NOT reorder
the dict.  This has been declared officially on python-dev.

> "the lists will directly correspond"? What does that mean? Why not
> "the lists will be equal i.e. list1 == list2"?
> How about "Provided that keys are neither added nor deleted, the order
> of iteration will not change"?

Python 3.0 never returns lists directly (it provides views), so the
wording has already been tweaked.

More information about the Python-list mailing list