Install & Compatibility
Where this runs
tested against v2.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 18MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.010s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SortedList
✓ from sortedcontainers import SortedList
Correct import path for SortedList.
SortedDict
✓ from sortedcontainers import SortedDict
Correct import path for SortedDict.
SortedSet
✓ from sortedcontainers import SortedSet
Correct import path for SortedSet.
A quickstart guide demonstrating the usage of SortedList, SortedDict, and SortedSet from the sortedcontainers library.
from sortedcontainers import SortedList, SortedDict, SortedSet
# SortedList
sl = SortedList(['e', 'a', 'c', 'd', 'b'])
print(sl) # Output: SortedList(['a', 'b', 'c', 'd', 'e'])
# SortedDict
sd = SortedDict({'c': -3, 'a': 1, 'b': 2})
print(sd) # Output: SortedDict({'a': 1, 'b': 2, 'c': -3})
# SortedSet
ss = SortedSet([5, 4, 3, 2, 1])
print(ss) # Output: SortedSet([1, 2, 3, 4, 5])
# Adding elements
sl.add('f')
print(sl) # Output: SortedList(['a', 'b', 'c', 'd', 'e', 'f'])
# Removing elements
sl.remove('a')
print(sl) # Output: SortedList(['b', 'c', 'd', 'e', 'f'])
# Accessing elements
print(sl[0]) # Output: 'b'
print(sl[-1]) # Output: 'f'
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sortedcontainers'
The `sortedcontainers` library has not been installed in the current Python environment.
fixpip install sortedcontainers
AttributeError: 'SortedList' object has no attribute 'append'
`SortedList` uses `add()` and `update()` methods to maintain sorted order, unlike standard Python lists which use `append()`.
fixmy_sorted_list.add(element)
TypeError: unhashable type: 'list'
`SortedSet` requires its elements to be hashable (like tuples or numbers), but an unhashable type such as a list was provided.
fixConvert unhashable elements to a hashable type (e.g., `tuple()`) before adding them to the `SortedSet`.
TypeError: '>' not supported between instances of 'dict' and 'dict'
Elements in a `SortedList` or keys in a `SortedDict` must be comparable, but the provided types (e.g., dictionaries or custom objects without comparison methods) do not have a default ordering.
fixDefine comparison methods (`__lt__`, `__gt__`) for custom objects, or provide a `key` function to the `SortedList` or `SortedDict` constructor to specify how elements should be ordered (e.g., `SortedList(key=lambda x: x['id'])`).
Upgrade
Version history
2.4.0latest on PyPI · released May 16, 2021
Audit
Dependencies
No dependency data recorded yet.