Both OrderedDict
and dict
are insertion-ordered¹ for iteration. There is practically no reason to use OrderedDict
if iteration order is the only deciding point, especially if re-ordering is not needed.
Obviously, if comparison order is desired OrderedDict
and dict
are not interchangeable.
Or phrased differently, for Python >=3.6, is there any reason to use OrderedDict
?
These days OrderedDict
is to dict
what deque
is to list
, basically. OrderedDict
/deque
are based on linked lists² whereas dict
/list
are based on arrays. The former have better pop/move/FIFO semantics, since items can be removed from the start/middle without moving other items.
Since arrays are generally very cache friendly, the linked list advantage only comes into play for very large containers. Also, OrderedDict
(unlike deque
) does not have guarantees for its linked list semantics and its advantage may thus not be portable. OrderedDict
should primarily be used if many pop/move/FIFO operations are needed and benchmarking can compare the performance of dict
vs. OrderedDict
in practice.
¹This applies to all currently supported implementations compliant with the Python language spec, i.e. CPython and PyPy since Python 3.6.
²OrderedDict
in CPython still preserves O(1) key access. This is realised by also having a "regular" lookup table, using the linked list for order between items and the lookup table for direct item access. It's complicated.