Why items order in a dictionary changed in Python?

Why items order in a dictionary changed in Python?

May I know why this is happening?

It is because of the way dicts are organized internally.

In short, this works via a hash-table which puts the keys into buckets according to their hash() value.

If I use dict.keys() to extract the keys from a dictionary and iterate it in an order that I suppose it to be, will that cause dismatch problem?

Depending on how you do it.

k = list(d.keys())
for i in k: print i, d[i]

should exactly work how you want it to work.

If you want to maintain the insertion order, you can use OrderedDict. But regular dict objects store the values by hashing the key and using that for lookups, which doesnt keep the order, but makes lookup faster.

Why items order in a dictionary changed in Python?

All you have to worry about is, you are getting value corresponding to the key or not and that is guaranteed by dict.

d={server:mpilgrim, database:master, uid:sa, pwd:secret}
for k, v in d.items():
    print k, v


pwd secret
database master
uid sa
server mpilgrim

Note: If you really want the order of the keys to be predictable, set PYTHONHASHSEED environmental variable an integer value in the range [0,4294967295]. Quoting from python --help

PYTHONHASHSEED: if this variable is set to random, the effect is the
same as specifying the -R option: a random value is used to seed
the hashes of str, bytes and datetime objects. It can also be set
to an integer in the range [0,4294967295] to get hash values with a
predictable seed.

Leave a Reply

Your email address will not be published. Required fields are marked *