pythonlanguage-designsortedsetsortedmap

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


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


Solution

  • It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the collections module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications.

    The basic concept is that if a user is sophisticated enough to realise that the builtin types aren't the right solution for their problem, then they're also up to the task of finding an appropriate third party library.

    Given that list+sorting, list+heapq and list+bisect cover many of the use cases that would otherwise rely on inherently sorted data structures, and packages like blist exist, there isn't a huge drive to add more complexity in this space to the standard library.

    In some ways, it is similar to the fact that there's no multi-dimensional array in the standard library, instead ceding that task to the NumPy folks.