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 sortedcollectionsVerified import paths — ran on the pinned version, not inferred.
Demonstrates the creation and basic usage of SortedDict and SortedList, showing how elements are automatically kept in sorted order upon insertion.
Migrate to using the public API methods (e.g., `__getitem__`, `__len__`, `islice`, `irange`, `item_at`) instead of accessing internal attributes.
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.
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.
pip install sortedcollections
from sortedcollections import SortedList
Ensure all items inserted into the collection are of compatible types, or define comparison methods (__lt__, __eq__, etc.) for custom objects.
You do not need to call sort(); the SortedList is always kept in sorted order automatically.
No dependency data recorded yet.