Sorted Containers
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))
Warnings
- breaking In version 2.4.0, SortedDict methods 'or', 'ror', and 'ior' were implemented per PEP 584. Ensure compatibility with existing codebases before upgrading.
- deprecated The 'iloc' attribute in SortedDict is deprecated. Use 'SortedDict.keys()' instead.
Install
-
pip install sortedcontainers
Imports
- SortedList
from sortedcontainers import SortedList
- SortedDict
from sortedcontainers import SortedDict
- SortedSet
from sortedcontainers import SortedSet
Quickstart
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'