Order of elements in a dict

Fredrik Lundh fredrik at pythonware.com
Tue Apr 26 05:24:57 EDT 2005


Marcio Rosa da Silva wrote:

> In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are 
> stored using its own rules.
>
> Ex:
>
> >>> d = {3: 4, 1: 2}
> >>> d
> {1: 2, 3: 4}
>
> So, my question is: if I use keys() and values() it will give me the keys and values in the same 
> order?

    http://docs.python.org/lib/typesmapping.html

    "Keys and values are listed in an arbitrary order which is non-random,
    varies across Python implementations, and depends on the dictionary's
    history of insertions and deletions. If items(), keys(), values(), iteritems(),
    iterkeys(), and itervalues() are called with no intervening modifications
    to the dictionary, the lists will directly correspond."

</F> 






More information about the Python-list mailing list