All chapters in this track
- 011. Variables, Types & Operators
- 022. Strings
- 033. Lists
- 044. Tuples, Sets & Dicts
- 055. Control Flow
- 066. Error Handling
- 077. Functions & Lambdas
- 088. Comprehensions
- 099. Built-in Functions
- 1010. Type Hints (3.10+)
- 1111. Classes & Dataclasses
- 1212. Iterators & Generators
- 1313. DSA Standard Library
- 1414. Python 3.9–3.11 Features for DSA
- 1515. DSA Idioms & Gotchas
- 1616. Master Quick Reference
Chapter 16 of 16
16. Master Quick Reference
Data Structures
| Structure | When to use | Create | Key ops |
|---|---|---|---|
list | Ordered, index-based | [1,2,3] | append, pop, sort |
dict | Key→value mapping | {} | d[k], d.get(k,v) |
set | Fast membership, unique | set() | add, discard, in |
tuple | Immutable, hashable | (a,b) | unpack, as dict key |
deque | Queue, double-ended buffer | deque() | appendleft, popleft |
defaultdict | Auto-init missing keys | defaultdict(list) | same as dict |
Counter | Frequency map | Counter(it) | .most_common(k), .total() |
heapq | Priority queue (min) | [] + heappush | heappush, heappop |
OrderedDict | LRU Cache | OrderedDict() | .move_to_end() |
Complexity
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
list | † end | |||
dict/set | † | |||
deque | mid | ends | ends | |
heapq | min | — | ||
sorted list | bisect | insort |
† Amortised
Built-ins Cheat Sheet
# Sorting
sorted(it, key=f, reverse=b) # new list
lst.sort(key=f) # in-place
# Iteration
enumerate(it, start=0) # (i, v) pairs
zip(a, b, strict=False) # paired; strict=True raises on mismatch (3.10+)
zip(*matrix) # transpose
# Aggregation
sum(it, start=0)
min(it, key=f) / max(it, key=f)
any(it) / all(it)
# Math
pow(b, e, mod) # fast modular exponentiation
divmod(a, b) # (quotient, remainder)
math.gcd(*args) # 3.9+: multi-arg
math.lcm(*args) # 3.9+: multi-arg
math.isqrt(n) # integer square root
math.comb(n, k) # C(n,k) — binomial
n.bit_count() # count set bits — 3.10+
bin(n); oct(n); hex(n) # base conversion, prefixed strings
int(s, base) # parse string in given base
format(n, '08b') # zero-padded binary string
# Strings
"".join(lst) # build string from list — O(n)
s.removeprefix(p) # 3.9+
s.removesuffix(p) # 3.9+
# Assignment expressions
while (n := len(x)) > 0: ... # walrus — 3.8+, assign + use in one expr
# Error handling
try:
...
except (KeyError, IndexError):
...
finally:
...
raise ValueError("msg") from e
# Iterators & generators
def gen(): yield x # lazy generator function
yield from sub_gen() # delegate to sub-generator
class It:
def __iter__(self): return self
def __next__(self):
if done: raise StopIteration
return next_value
Standard Library for DSA
| Module | Import | Key items |
|---|---|---|
collections | from collections import … | deque, defaultdict, Counter, OrderedDict |
heapq | import heapq | heappush, heappop, heapify, nlargest, nsmallest |
bisect | import bisect | bisect_left, bisect_right, insort_left; key= 3.10+ |
math | import math | gcd, lcm, isqrt, comb, inf, cbrt(3.11+), exp2(3.11+) |
functools | from functools import … | cache(3.9+), lru_cache, cmp_to_key, reduce |
itertools | from itertools import … | accumulate, combinations, permutations, product, chain |
sys | import sys | setrecursionlimit, stdin.readline |
Modern Python (3.9–3.11) DSA Cheat Sheet
# 3.9+
list[int] # built-in generic hints
dict[str,int]
d1 | d2 # dict merge
s.removeprefix("pre_")
s.removesuffix("_suf")
math.lcm(a, b, c)
math.gcd(a, b, c)
@cache # functools.cache
# 3.10+
int | None # union type hint (replaces Optional[int])
n.bit_count() # popcount
Counter(it).total()
zip(a, b, strict=True)
bisect.bisect_left(data, v, key=lambda x: x[0])
match val:
case pattern: ...
# 3.11+
from typing import Self # self-reference in class methods
math.cbrt(x) # cube root
math.exp2(x) # 2^x
# ~60% faster than 3.10 overall
End of Python 3.11+ Syntax for DSA — August 2026