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

John Machin sjmachin at lexicon.net
Sun Nov 23 20:43:37 EST 2008


On Nov 24, 11:59 am, Carsten Haese <carsten.ha... at gmail.com> 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 quothhttp://docs.python.org/library/stdtypes.html#mapping-types-dict:
>
> """
> 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.

"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"?






More information about the Python-list mailing list