1

Per python specification, dictionaries are ordered since 3.7. This is discussed here, for example: https://stackoverflow.com/a/39980744/1576254

My question is: I still see OrderedDict in library implementations, and checking on the content of collections.OrderedDict in Python3.8 I see that it's not aliased to the built-in dict.

Does the collections.OrderedDict have any advantages to built-in dict? does it have any differences at all that can be seen by a user? (it's clear it's a different class, which is checkable at runtime, meaning there is at least this difference)

ikamen
  • 3,175
  • 1
  • 25
  • 47
  • 1
    This is addressed in the documentation: https://docs.python.org/3/library/collections.html#ordereddict-objects – Kenny Ostrom Dec 27 '21 at 17:01
  • Does this answer your quesetion? https://stackoverflow.com/questions/50872498/will-ordereddict-become-redundant-in-python-3-7 – Frank Vel Dec 27 '21 at 17:02

1 Answers1

3

From this link: https://realpython.com/python-ordereddict/ it tells three differences between OrderedDict and a built-in dict.

  1. 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

  2. Control over the order of items: If you need to rearrange or reorder the items in a dictionary, then you can use .move_to_end() and also the enhanced variation of .popitem()

  3. Equality test behavior: If your code compares dictionaries for equality, and the order of items is important in that comparison, then OrderedDict is the right choice

The OrderedDict is a subset class of dict, as dict is a class in python. I'd also recommend looking through the rest of the article above because it has some other interesting information.