Will OrderedDict become redundant in Python 3.7?

13,535

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

Share:
13,535

Related videos on Youtube

James Hiew
Author by

James Hiew

Software developer interested in distributed systems and tools which make programming more productive. Use Python, Java and Go, also interested in Rust!

Updated on January 06, 2022

Comments

  • James Hiew
    James Hiew over 2 years

    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.

  • Tim Skov Jacobsen
    Tim Skov Jacobsen over 3 years
    The point about order sensitivity when comparing is very valid here.
  • leggewie
    leggewie almost 3 years
    Thank you very much for the explanation. I believe that with 3.8, there is no such difference anymore.
  • Chris_Rands
    Chris_Rands almost 3 years
    @leggewie In Python 3.8 (and in 3.9), the point about equality comparisons is still different between OrderedDict and regular dict- I do not expect this to change anytime soon