Python: hash, id and Dictionary Order

TL;DR - really, seriously, no kidding, don’t even think about relying on dictionary traverse order, because it may change even from seemingly unrelated code changes.

This is a companion discussion topic for the original entry at

So wired and so interesting, i did know that im not suppose to rely on dick's order, but now i know i wont even dera to try!

I believe the reason for the change is that if you add some lines, the bytecode for the module grows, and of course the strings "pdb" and "set_trace" (or "time" and "sleep") have to be added to the module's list of constants. Naturally, memory for the module has to be allocated before the module runs, so any further allocations are done somewhere else.

Another comment ­ ­ – in Python 3, dict ordering will be different in each execution of Python, even if the code doesn't change. You can get the behavior already for 2.6+ by setting PYTHONHASHSEED (see -- it prevents some DOS attacks on web servers, for example.

If you want to rely on items order in dictionary then OrderedDict is your friend.

I know, I suggested it in the end of the post :)