Ordered data structures are very useful in programming, especially in algorithm competitions and competitive programming. In Python, the Sorted Containers library mainly provides three such data structures: SortedDict, SortedSet, and SortedList.
Mastering Python’s Sorted Data Structures: From Built-ins to sortedcontainers
A Complete Guide to Sorted Data Structures in Python
When and How to Use Sorted Lists, Dicts, and Sets in Python
Python Sorted Data Structures Explained with Real-World Examples
Sorted Containers vs Built-ins: The Python Developer’s Guide to Ordered Data
Python Sorted Data Structures Tutorial
Sorting is one of the most common operations in programming. Python provides several ways to maintain sorted data, from built-in tools like lists, sets, and heaps, to advanced third-party libraries like sortedcontainers.
This tutorial will walk through Python’s sorted data structures, their strengths and weaknesses, and real-world usage cases.
Built-in Options
1. list + bisect
You can keep a list sorted manually using the bisect module.
import bisect
nums = [1, 3, 5]
bisect.insort(nums, 2) # insert while keeping sorted order
print(nums) # [1, 2, 3, 5]
This works but insertions and deletions are O(n) since shifting elements is required.
2. heapq
The heapq module implements a priority queue. It keeps elements partially ordered so the smallest (or largest) can be retrieved efficiently.
import heapq
nums = [5, 1, 3]
heapq.heapify(nums) # turn into heap
print(heapq.heappop(nums)) # 1
This is efficient for repeated min/max queries, but the entire list is not fully sorted.
3. dict and set
Dictionaries and sets in Python use hash tables. They provide O(1) lookups but do not keep elements in sorted order.
myset = {3, 1, 2}
print(2 in myset) # True
print(sorted(myset)) # [1, 2, 3]
For true sorted behavior, we need something better.
The sortedcontainers Library
The sortedcontainers library provides three powerful data structures:
- SortedList
- SortedDict
- SortedSet
They maintain sorted order automatically with efficient O(log n) insertions and deletions.
Install it with:
pip install sortedcontainers
SortedList
Like a list, but always sorted.
from sortedcontainers import SortedList
sl = SortedList([5, 1, 3])
print(sl) # SortedList([1, 3, 5])
sl.add(2)
print(sl) # SortedList([1, 2, 3, 5])
sl.remove(3)
print(sl) # SortedList([1, 2, 5])
print(sl.bisect_left(2)) # 1
Usage: running statistics, sliding window median, leaderboards.
SortedDict
Like dict, but keys are sorted.
from sortedcontainers import SortedDict
sd = SortedDict({"b": 2, "a": 1, "c": 3})
print(sd) # SortedDict({'a': 1, 'b': 2, 'c': 3})
print(sd.peekitem(0)) # ('a', 1)
print(sd.peekitem(-1)) # ('c', 3)
for k in sd:
print(k, sd[k])
Usage: ordered maps, interval scheduling, nearest neighbor lookup.
SortedSet
Like set, but sorted.
from sortedcontainers import SortedSet
ss = SortedSet([3, 1, 2, 2])
print(ss) # SortedSet([1, 2, 3])
ss.add(5)
print(ss) # SortedSet([1, 2, 3, 5])
print(ss.bisect_left(3)) # 2
Usage: deduplicated logs, sorted unique values.
Real-World Examples
1. Sliding Window Median
from sortedcontainers import SortedList
def sliding_window_median(nums, k):
sl = SortedList()
res = []
for i, num in enumerate(nums):
sl.add(num)
if i >= k:
sl.remove(nums[i-k])
if i >= k-1:
if k % 2 == 1:
res.append(sl[k//2])
else:
res.append((sl[k//2-1] + sl[k//2]) / 2)
return res
print(sliding_window_median([1,3,-1,-3,5,3,6,7], 3))
2. Leaderboard System
from sortedcontainers import SortedList
class Leaderboard:
def __init__(self):
self.scores = SortedList()
def add_score(self, player, score):
self.scores.add((score, player))
def top(self, n):
return self.scores[-n:]
def reset(self, player, score):
self.scores.remove((score, player))
lb = Leaderboard()
lb.add_score("Alice", 50)
lb.add_score("Bob", 80)
lb.add_score("Charlie", 70)
print(lb.top(2))
3. Nearest Key with SortedDict
from sortedcontainers import SortedDict
sd = SortedDict({"a": 1, "c": 3, "e": 5})
idx = sd.bisect_left("d")
print("Predecessor:", sd.peekitem(idx-1))
print("Successor:", sd.peekitem(idx))
When to Use Which
- list + bisect: static data, rare updates
- heapq: repeated min/max queries
- dict/set: fast lookups without sorted order
- sortedcontainers: sorted order with frequent updates
| Feature | list + bisect | heapq | dict | set | sortedcontainers |
|---|---|---|---|---|---|
| Maintains sorted order | Yes (manual) | No | No | No | Yes |
| Insert/remove complexity | O(n) | O(log n) | O(1) | O(1) | O(log n) |
| Random access | Yes | No | Keys only | No | Yes |
| Range queries | No | No | No | No | Yes |
| Best use-case | Static search | Priority queue | Hash map | Membership tests | Sorted collections with updates |
Conclusion
The sortedcontainers library gives you efficient and elegant sorted data structures in Python. If you need both order and performance, it is often the best choice compared to built-in tools.
–EOF (The Ultimate Computing & Technology Blog) —
1018 wordsLast Post: How would you do Binary Search to Locate a Block given a TimeStamp (for Any Blockchain)?
Next Post: A Complete Guide to Radix Sort in Python with Examples