SortedList is in the sortedcontainers module, not the standard library

SortedList does not come built into Python. You need to install the sortedcontainers package first, then import it into your code. The standard library has no sorted list data structure — SortedList fills that gap by keeping items in order automatically as you add or remove them.

Once installed, you import it with a single line: from sortedcontainers import SortedList. After that, you create a new sorted list the same way you create any other Python object.

Key Takeaways

  • Install sortedcontainers using pip with the command pip install sortedcontainers before you can use SortedList.
  • Import SortedList into your script with from sortedcontainers import SortedList at the top of your file.
  • SortedList keeps items in sorted order automatically, so you do not have to call sort() after each addition.
  • You can initialize a SortedList with existing data, like SortedList([3, 1, 2]), and it will sort the items for you.

Install sortedcontainers using pip

Open your terminal or command prompt and run pip install sortedcontainers. This downloads and installs the package into your Python environment. If you use a virtual environment (which is common for projects), make sure that environment is active before you run the command.

On Windows, you may need to use python -m pip install sortedcontainers instead. On macOS or Linux, you might need pip3 if you have both Python 2 and Python 3 installed. The package installs in seconds and has no dependencies — it does not require other packages to work.

Import SortedList at the top of your script

Add this line to the very beginning of your Python file, before any code that uses SortedList:

from sortedcontainers import SortedList

You can also import the entire module and reference SortedList by its full name: import sortedcontainers, then use sortedcontainers.SortedList() later. The first approach is shorter and more common. If you need other classes from the same package, like SortedDict or SortedSet, import them on the same line: from sortedcontainers import SortedList, SortedDict, SortedSet.

Create a SortedList with or without initial data

To create an empty sorted list, write my_list = SortedList(). To create one with existing items, pass them as a list: my_list = SortedList([5, 2, 8, 1]). The SortedList constructor sorts the items automatically, so you do not need to call sort() yourself.

You can also pass any iterable — a tuple, a set, or even the result of a function that returns items one at a time. SortedList will convert it to a sorted list internally. If you pass duplicate values, SortedList keeps them all, just like a regular Python list.

Add and remove items while maintaining order

Use the add() method to insert a single item: my_list.add(3). SortedList finds the correct position and inserts it there, so the list stays sorted without extra work from you. Use update() to add multiple items at once: my_list.update([7, 2, 9]). Both methods maintain sorted order automatically.

Remove items with remove(), which takes the value you want to delete: my_list.remove(3). If the value appears multiple times, remove() deletes only the first occurrence. Use pop() to remove by position, like my_list.pop(0) to remove the first item or my_list.pop(-1) to remove the last. Use clear() to empty the entire list at once.

Access items by index and search efficiently

SortedList supports indexing just like a regular list. my_list[0] returns the smallest item, my_list[-1] returns the largest, and my_list[2] returns the third item. You can also slice: my_list[1:4] returns items at positions 1, 2, and 3.

SortedList also has a bisect_left() method that finds where a value would go in the sorted order without actually inserting it. This is faster than searching through the whole list yourself. my_list.bisect_left(5) returns the index where 5 would be inserted. Use bisect_right() if you want the position after any existing copies of that value.

Common mistakes and how to avoid them

The most common mistake is forgetting to install sortedcontainers before trying to import it. If you see an error like ModuleNotFoundError: No module named 'sortedcontainers', go back and run pip install sortedcontainers. Make sure you are in the right Python environment if you use virtual environments.

Another mistake is trying to modify items in place. SortedList does not support assignment like my_list[0] = 10 because changing an item could break the sorted order. Instead, remove the old item and add the new one: my_list.remove(old_value) then my_list.add(new_value). This keeps the list sorted and prevents silent bugs.

Frequently Asked Questions

Do I need to import SortedList every time I run my script?

No. You write the import statement once at the top of your file, and it stays there. Every time you run the script, Python reads that import and makes SortedList available. You do not need to reinstall the package each time — pip install is a one-time setup step.

Can I use SortedList with custom objects, not just numbers and strings?

Yes, but your custom objects need to be comparable. Python needs to know whether one object is "less than" another. Define the __lt__() method in your class, or pass a key function to SortedList that tells it how to compare items. For example, SortedList(people, key=lambda p: p.age) sorts people by age.

What is the difference between SortedList and just using sort()?

A regular list with sort() is sorted once, at the moment you call sort(). If you add new items later, they go at the end unsorted. SortedList keeps items sorted automatically every time you add or remove something. Use SortedList when you need the list to stay sorted throughout your program's run.

Does SortedList work with negative numbers or mixed types?

SortedList works fine with negative numbers — they sort correctly alongside positive ones. Mixing types like integers and strings usually fails because Python cannot compare them. Keep all items in a SortedList the same type, or use a custom key function to define how different types should be ordered.