Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will OrderedDict become redundant in Python 3.7?

From the Python 3.7 changelog:

the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.

Would this mean that OrderedDict will become redundant? The only use I can think of it will be to maintain backwards compatibility with older versions of Python which don't preserve insertion-order for normal dictionaries.

like image 394
James Hiew Avatar asked Jun 15 '18 09:06

James Hiew


People also ask

Is OrderedDict slower than dict?

OrderedDict is over 80% slower than the standard Python dictionary (8.6/4.7≈1.83).

When should I use OrderedDict?

Intent signaling: If you use OrderedDict over dict , then your code makes it clear that the order of items in the dictionary is important. You're clearly communicating that your code needs or relies on the order of items in the underlying dictionary.

What does OrderedDict mean in Python?

An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that: OrderedDict preserves the order in which the keys are inserted.

What is difference between dict and OrderedDict Python?

The OrderedDict is a subclass of dict object in Python. The only difference between OrderedDict and dict is that, in OrderedDict, it maintains the orders of keys as inserted. In the dict, the ordering may or may not be happen. The OrderedDict is a standard library class, which is located in the collections module.


1 Answers

No it won't become redundant in Python 3.7 because OrderedDict is not just a dict that retains insertion order, it also offers an order dependent method, OrderedDict.move_to_end(), and supports reversed() iteration*.

Moreover, equality comparisons with OrderedDict are order sensitive and this is still not the case for dict in Python 3.7, for example:

>>> OrderedDict([(1,1), (2,2)]) == OrderedDict([(2,2), (1,1)])  False >>> dict([(1,1), (2,2)]) == dict([(2,2), (1,1)])  True 

Two relevant questions here and here.

* Support for reversed() iteration of regular Python dict is added for Python 3.8, see issue33462

like image 181
Chris_Rands Avatar answered Oct 12 '22 12:10

Chris_Rands