6

OrderedDict is a collection that preserves order of items (in which they were inserted). I do not think this can be achieved with hashtables (which regular dict uses I think) so are balanced trees used to implement it?

I assume there are many depends like Python 2/3 and CPython/IPython/others.

ArekBulski
  • 4,520
  • 4
  • 39
  • 61

1 Answers1

26

You can read the implementation in CPython's source code: Lib/collections/__init__.py as OrderedDict is implemented in Python.

It uses a doubly linked list to maintain the order of the elements in the dictionary.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180