Essentially, a dictionary does not have the order in which it stores keys. Therefore, you cannot rely on an order. (I would not assume that the order will not change, even if the environment is identical).
One of the few reliable ways:
for key in sorted(yourDictionary.keys()):
EDIT : response to your comment: Python does not store keys randomly. All documentation states that you should not rely on this order . It depends on how key management is done. What I will say here about your question: if you rely on this order, you are probably doing something wrong. In general, you donβt need to rely on this at all. :-)
source share