pythonlistsorting

Does python have a sorted list?


By which I mean a structure with:

I also had a related question about performance of list(...).insert(...) which is now here.


Solution

  • The standard Python list is not sorted in any form. The standard heapq module can be used to append in O(log n) to an existing list and remove the smallest one in O(log n), but isn't a sorted list in your definition.

    There are various implementations of balanced trees for Python that meet your requirements, e.g. rbtree, RBTree, or pyavl.