Sorted Containers

2.4.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

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'

view raw JSON →