SortedList is a data structure from the sortedcontainers module that keeps items in sorted order automatically

Python's built-in list does not maintain sorted order on its own — you have to sort it manually each time you add or remove items. SortedList handles that for you. Every time you add an item, it finds the right position and inserts it there. Every time you remove an item, the list stays sorted without extra work.

To use SortedList, you first install the sortedcontainers module (which does not come with Python by default), then import the SortedList class into your code. After that, you create a SortedList object the same way you would create a regular list, and use methods like add() and remove() instead of append().

Key Takeaways

  • Install sortedcontainers using pip with the command pip install sortedcontainers before you can import SortedList.
  • Import SortedList into your code with from sortedcontainers import SortedList at the top of your file.
  • Create a new SortedList with my_list = SortedList() or initialize it with existing items using SortedList([3, 1, 2]).
  • Add items with the add() method and remove them with remove() or pop() — the list stays sorted automatically.

Install sortedcontainers before importing

The sortedcontainers module does not ship with Python, so you need to read it first. Open your terminal or command prompt and run pip install sortedcontainers. This downloads the module and installs it into your Python environment.

If you are working in a virtual environment (which is a good practice), make sure that environment is activated before you run pip. If you use conda instead of pip, run conda install sortedcontainers. Either way, you only need to install it once per environment.

Import SortedList at the top of your file

Once sortedcontainers is installed, add this line to the very top of your Python file, before any other code that uses SortedList:

from sortedcontainers import SortedList

This imports the SortedList class so you can use it in your code. If you want to import other classes from the same module (like SortedDict or SortedSet), you can add them to the same line: from sortedcontainers import SortedList, SortedDict, SortedSet.

Create and populate a SortedList

After importing, create a new SortedList object with my_list = SortedList(). This creates an empty sorted list. You can also initialize it with existing items: my_list = SortedList([5, 2, 8, 1]) will create a SortedList containing those numbers, automatically sorted as [1, 2, 5, 8].

Add new items with the add() method: my_list.add(3) inserts 3 into the correct position. The list automatically stays sorted, so you do not have to call a separate sort function. You can check if an item is in the list with 3 in my_list, just like a regular list.

Remove items and access elements

Remove an item by value with my_list.remove(3). If the item appears more than once, this removes only the first occurrence. To remove by position instead, use my_list.pop(0) to remove the first item or my_list.pop(-1) to remove the last.

Access items by index the same way you would with a regular list: my_list[0] gives you the smallest item, my_list[-1] gives you the largest, and my_list[2] gives you the third item. You can also loop through a SortedList with for item in my_list: and the items will come out in sorted order.

When to use SortedList instead of a regular list

Use SortedList when you need items to stay sorted as you add and remove them throughout your program's run. If you only sort once at the end, a regular list with sort() is faster. But if you are constantly inserting and removing items and need to know they are always in order, SortedList saves you from calling sort repeatedly.

SortedList is also useful when you need to find items by rank (like "give me the 10th smallest value") or when you need to work with ranges of sorted values. For straightforward programs that just need a list of items, a regular list is fine. For data that must stay sorted as it changes, SortedList is the right choice.

Frequently Asked Questions

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

No. You install sortedcontainers once per Python environment with pip. After that, every Python file in that environment can import SortedList by adding from sortedcontainers import SortedList at the top. You do not reinstall it each time you run a program.

What happens if I add duplicate values to a SortedList?

SortedList keeps all duplicates. If you add 5 three times, all three 5s stay in the list in sorted order. When you call remove(5), only the first 5 is removed. If you need to track how many of each item you have, consider using a SortedDict with counts instead.

Is SortedList slower than a regular list?

Adding and removing items from a SortedList is slower than appending to a regular list, because SortedList has to find the right position first. But if you would otherwise sort a regular list after every change, SortedList is faster overall. For small lists (under 1,000 items), the difference is usually not noticeable.

Can I sort a SortedList in reverse order?

SortedList sorts in ascending order by default. To reverse the order, create it with a custom key: SortedList([3, 1, 2], key=lambda x: -x) sorts numbers from largest to smallest. You can also use any custom comparison function as the key.