Registry / data / sortedcollections

sortedcollections

JSON →
library2.1.0pypypi✓ verified 90d ago

Sorted Collections provides CPython-optimized mutable sorted collections (SortedList, SortedDict, SortedSet) that maintain their order automatically. As of version 2.1.0, it targets Python 3.7+ and is actively maintained, with releases typically following bug fixes or minor enhancements to ensure stability and performance.

pip install sortedcollections
INSTALL
IMPORT
SIG · SORTEDCOLLECTIONS
S
sortedcollections
datapythonv2.1.0
Install
1.6s avg
Import
17ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.1.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
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.017s · 18.1MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.016s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SortedList
✓ from sortedcollections import SortedList
SortedDict
✓ from sortedcollections import SortedDict
SortedSet
✓ from sortedcollections import SortedSet

Demonstrates the creation and basic usage of SortedDict and SortedList, showing how elements are automatically kept in sorted order upon insertion.

from sortedcollections import SortedDict sd = SortedDict() sd[5] = 'apple' sd[1] = 'banana' sd[3] = 'cherry' print(f"SortedDict items: {list(sd.items())}") # Expected output: SortedDict items: [(1, 'banana'), (3, 'cherry'), (5, 'apple')] # Example with SortedList from sortedcollections import SortedList sl = SortedList([5, 1, 3, 2, 4]) sl.add(0) print(f"SortedList: {list(sl)}") # Expected output: SortedList: [0, 1, 2, 3, 4, 5]
Debug
Known issues
breakingDirect access to internal, non-public attributes like `_list` on `SortedList` or `SortedSet` was removed in version 2.0.0. Code that relied on these internal implementations will break.
fix
Migrate to using the public API methods (e.g., `__getitem__`, `__len__`, `islice`, `irange`, `item_at`) instead of accessing internal attributes.
affects: >=2.0.0
gotchaModifying mutable elements *in-place* within a `SortedList` or `SortedSet` can corrupt the collection's sort order if the modification changes the element's comparison value. The collection will not automatically re-sort.
fix
If an element's sort-defining properties change, remove the element and then re-add it to the collection to ensure it is correctly positioned. For immutable elements, this is not an issue.
affects: All
gotchaWhile highly optimized, many operations (e.g., insertion, deletion, lookup by value) on `SortedList`, `SortedSet`, and `SortedDict` have logarithmic time complexity (O(log N)) due to the necessity of maintaining sorted order. This differs from O(1) for some operations in standard, unsorted `list` or `dict`.
fix
Be mindful of these performance characteristics, especially in performance-critical loops or with very large datasets. Profile your application to ensure sorted collections meet your performance needs.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sortedcollections'
The 'sortedcollections' package has not been installed in the current Python environment.
fix
pip install sortedcollections
from sortedcollections import sortedlist
The class names in the 'sortedcollections' library are 'SortedList', 'SortedDict', and 'SortedSet' (title case), not lowercase.
fix
from sortedcollections import SortedList
TypeError: '<' not supported between instances of 'type1' and 'type2'
Items added to SortedList, SortedDict, or SortedSet must be mutually comparable using Python's default comparison operators.
fix
Ensure all items inserted into the collection are of compatible types, or define comparison methods (__lt__, __eq__, etc.) for custom objects.
AttributeError: 'SortedList' object has no attribute 'sort'
SortedList automatically maintains its sorted order upon item insertion, so it does not have or require an explicit sort() method.
fix
You do not need to call sort(); the SortedList is always kept in sorted order automatically.
Upgrade
Version history
2.1.0latest on PyPI · released Jan 18, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
sortedcollections — pip install sortedcollections · libregistry