[Tutor] Reversed dictionary returned by default
Danny Yoo
dyoo at hashcollision.org
Fri Mar 1 21:01:25 CET 2013
> Why is this happening, and how do I get it to work properly (returning
> element 0 - n, versus n - 0)?
Python dictionaries are unordered collections. When we iterate across
them, we can not guarantee that we'll walk the elements in the order
that we inserted them. The reason for that is because of the
underlying algorithm behind dictionaries, which is the hash table data
structure:
http://en.wikipedia.org/wiki/Hash_table
where the keys are deliberately scattered evenly through the
underlying array, in what looks like "random" order, so that later
lookups can be very, very fast.
For your application, does it matter that you preserve some kind of
order of the key-value pairs?
More information about the Tutor
mailing list