gotchaMinorpending
Python dictionary ordering -- insertion order preserved since 3.7
Viewed 0 times
dict orderingOrderedDictinsertion order3.7language guarantee
python
Error Messages
Problem
Old Python code uses OrderedDict everywhere because dictionaries were unordered. In modern Python, regular dicts preserve insertion order but some developers do not know this.
Solution
Since Python 3.7, dict preserves insertion order as a language guarantee (was implementation detail in 3.6). Use regular dict instead of OrderedDict. OrderedDict is only needed when: (1) order-sensitive equality comparison is needed (OrderedDict considers order, dict does not). (2) move_to_end() or popitem(last=True/False) methods are needed.
Why
CPython 3.6 changed dict implementation to a compact, ordered design. Python 3.7 made this a language specification guarantee, not just an implementation detail.
Revisions (0)
No revisions yet.