Why are there no sorted containers in Python's standard libraries?

Neil G picture Neil G · May 10, 2011 · Viewed 48.6k times · Source

Is there a Python design decision (PEP) that precludes a sorted container from being added to Python?

(OrderedDict is not a sorted container since it is ordered by insertion order.)

Answer

GrantJ picture GrantJ · Mar 21, 2014

There's also a python sortedcontainers module that implements sorted list, dict, and set types. It's very similar to blist but implemented in pure-Python and in most cases faster.

>>> from sortedcontainers import SortedSet
>>> ss = SortedSet([3, 7, 2, 2])
>>> ss
SortedSet([2, 3, 7])

It also has functionality uncommon to other packages:

>>> from sortedcontainers import SortedDict
>>> sd = SortedDict((num, num) for num in range(100000))
>>> sd.iloc[-5] # Lookup the fifth-to-last key.
99995

Disclaimer: I am the author of the sortedcontainers module.