4 ms·
The title of the post is misleading: it means "ordered by insertion order" (not sorted). Previously it was unpredictable, and that's why collections have Order
by reidrac 7y ago
The title of the post is misleading: it means "ordered by insertion order" (not sorted).
Previously it was unpredictable, and that's why collections have OrderedDict; which makes sense instead of trusting an implementation detail of CPython from 3.6.
Is not mentioned in the docs: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict https://docs.python.org/3/library/stdtypes.html#mapping-type...
EDIT: but the behaviour is documented in OrderedDict itself! https://docs.python.org/3/library/collections.html?highlight=ordereddict#ordereddict-objects https://docs.python.org/3/library/collections.html?highlight...
Interesting!
- fyp 7y agoI wonder why OrderedDict isn't outright deprecated (or reimplemented as a trivial wrapper over dict). You can get the first/last key of a 3.8 dict with dict.keys() and reversed(dict.keys()) which you can then delete to reimplement OrderedDict.popitem. Deleting a key and reinserting it will let you reimplement move_to_end. The only cool thing that OrderedDict's implementation might be useful for is to move to front (or anywhere else not the back) but it doesn't expose that api.
- deniska 7y agoWhen OrderedDict is compared with another OrderedDict, it also checks whether the order of items is the same (this way also violating LSP, but that's another story), dict instances don't do that.
- travisjungroth 7y agoThanks, I just learned something.