OrderedDict module's OrderedDict keeps track of the order of adding “key-value” pairs to the dictionary. What for? An ordinary dictionary does the same.

 avto ={} avto['alex'] = 'bmw' avto['masha'] = 'vaz' avto['jens'] = 'toyota' avto['jonh'] = 'volga' print(avto) for name, avt in avto.items(): print(name.title() + "'s " + avt.title() + ".") 
  • I think in the usual dictionary order is not guaranteed - VladD
  • Thanks for the answer. But in fact it turns out that a simple dictionary adds strictly in the order that was given. - Alexander
  • In fact, it is not that it turns out , but rather it turned out on this data, on this system and on this implementation / version of the language . - VladD
  • Worse: if I am not mistaken, then two successive iterations do not guarantee the same order too - VladD

2 answers 2

In one of the latest versions of the interpreter, the internal mechanics of the dictionary has been updated. This was done to reduce the memory occupied by the dictionary, but one of the side effects was maintaining order.

However, you need to understand that this is just a side effect, which is present only in some versions of a particular interpreter.

If you write a program that implies that the dictionary must maintain its order, then it will not work correctly on older versions of the interpreter, or on other interpreters, and perhaps even in some future versions of the interpreter.

Therefore, if it is important for your program that the dictionary maintains order, it is recommended to use OrderedDict - it guarantees that order is maintained on other interpreters.