Since Python 3.6 dict
has maintained order after implementation of a new compact dictionary type.
What's New in Python 3.6 -- New dict implementation
The dict type now uses a “compact” representation based on a proposal by Raymond Hettinger which was first implemented by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5.
The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).
As of Python 3.7 the above was no longer implementation detail, and dict
maintaining order became an official language detail.
[Python-Dev] Guarantee ordered dict literals in v3.7?
Make it so. "Dict keeps insertion order" is the ruling. Thanks!