Why python3.6+ save OrderedDict?

Hi, everyone!
I just know that after Python 3.6, Dict use a new struct and it become ordered, so why we still save collections.OrderedDict now? Just for compatibility? I think it may have more difference between Dict and OrderedDict.
Moreover, can you tell me what difference between collections.OrderedDict and odictobject in pythoncore/objects ?
Thank you !

In Python 3.6, insertion order is preserved in dicts, but this is considered a CPython implementation detail. In Python 3.7, it is officially guaranteed at the language level, such that other Python 3.7 interpreters (PyPy, Stackless, etc) are also required to preserve insertion order. However, note all this does is preserve insertion order, and many differences still remain, as summarized right at the very top of the OrderedDict documentation, so I won’t repeat any of them here; next time, please remember to check the documentation for a topic before asking others to do so for you, as not only will it save us time, but is less work for you than typing up a question, and will get you a much faster answer. Thanks.

The former, at least the type actually defined in Lib/collections/__init__.py, is the pure-Python implementation, while the latter is the C implementation, though note that referencing collections.OrderedDict in your own code may be using the latter under the hood. In general, the latter will be much faster and more efficient, but less portable. Beyond that, the source code for the latter explains the implementation and differences in great detail, so I’d suggest you read that if you are curious about this.

1 Like