Sorted Containers

raw JSON →
2.4.0 verified Tue May 12 auth: no python install: verified quickstart: verified

A pure-Python library providing fast and easy-to-use implementations of SortedList, SortedDict, and SortedSet data types. Current version: 2.4.0. Release cadence: approximately every 87 days. ([python.libhunt.com](https://python.libhunt.com/sorted_containers-changelog?utm_source=openai))

pip install sortedcontainers
error ModuleNotFoundError: No module named 'sortedcontainers'
cause The `sortedcontainers` library has not been installed in the current Python environment.
fix
pip install sortedcontainers
error AttributeError: 'SortedList' object has no attribute 'append'
cause `SortedList` uses `add()` and `update()` methods to maintain sorted order, unlike standard Python lists which use `append()`.
fix
my_sorted_list.add(element)
error TypeError: unhashable type: 'list'
cause `SortedSet` requires its elements to be hashable (like tuples or numbers), but an unhashable type such as a list was provided.
fix
Convert unhashable elements to a hashable type (e.g., tuple()) before adding them to the SortedSet.
error TypeError: '>' not supported between instances of 'dict' and 'dict'
cause 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.
fix
Define 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'])).
breaking In version 2.4.0, SortedDict methods 'or', 'ror', and 'ior' were implemented per PEP 584. Ensure compatibility with existing codebases before upgrading.
fix Review and update codebases to accommodate the new methods in SortedDict.
deprecated The 'iloc' attribute in SortedDict is deprecated. Use 'SortedDict.keys()' instead.
fix Replace usage of 'iloc' with 'keys()' in SortedDict.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.01s 18.0M
3.10 slim (glibc) - - 0.01s 19M
3.11 alpine (musl) - - 0.02s 19.9M
3.11 slim (glibc) - - 0.01s 20M
3.12 alpine (musl) - - 0.01s 11.8M
3.12 slim (glibc) - - 0.01s 12M
3.13 alpine (musl) - - 0.01s 11.4M
3.13 slim (glibc) - - 0.01s 12M
3.9 alpine (musl) - - 0.01s 17.5M
3.9 slim (glibc) - - 0.01s 18M

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'