In what order does Python read dictionary keys?

Martijn Faassen faassen at pop.vet.uu.nl
Tue Jun 8 16:43:35 EDT 1999


tcpeter at my-deja.com wrote:
> 
> Hello, all.  I've checked the docs and queried DejaNews for this but I
> couldn't come up with an answer.  I wrote a script to convert rgb
> values to hex for when I'm doing colors in HTML pages and don't have
> Photoshop handy.  The code appears below.  My question is, how does
> Python decide in which order to read the keys?  Notice that I have the
> dictionary set up in this order: red, blue, green.  (I mistyped it while
> writing the code).  But when I ran the code, the options were presented
> in the desired order. (A trifle creepy, if you ask me.  Turing would be
> delighted.)  Anyway, how does Python decide the order. Thanks.

Hi there,

The answer is that Python doesn't have any set orders for dictionaries.
The internal hashing algorithm the python dictionary uses has no
guaranteed order. So basically you shouldn't depend on any particular
order it produces.

Regards,

Martijn




More information about the Python-list mailing list