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 14 of 16
14. Python 3.9–3.11 Features for DSA
Feature Summary with DSA Applicability
# ── 3.9 ─────────────────────────────────────────────────────────
# Built-in generics in hints (no need to import from typing)
def f(arr: list[int]) -> dict[str, set[int]]: ...
# dict merge operator
merged = d1 | d2 # new dict
d1 |= d2 # update in-place
# str.removeprefix / removesuffix
"test_foo".removeprefix("test_") # "foo"
"foo_test".removesuffix("_test") # "foo"
# math.gcd / lcm with multiple args
math.gcd(12, 18, 24) # 6
math.lcm(4, 6, 9) # 36
# ── 3.10 ─────────────────────────────────────────────────────────
# X | Y union type syntax
def f(x: int | str | None) -> bool | None: ...
# match / case — structural pattern matching
match point:
case (0, 0): print("origin")
case (x, 0): print(f"x-axis at {x}")
case (0, y): print(f"y-axis at {y}")
case (x, y): print(f"({x}, {y})")
# zip(strict=True) — raises if lengths differ
for a, b in zip(lst1, lst2, strict=True): ...
# int.bit_count() — number of set bits
(0b1010).bit_count() # 2
n.bit_count() # replaces bin(n).count('1')
# Counter.total() — sum of all counts
Counter("aabbc").total() # 5
# bisect with key (3.10+)
bisect.bisect_left(data, val, key=lambda x: x[0])
# TypeAlias
from typing import TypeAlias
Adj: TypeAlias = dict[int, list[tuple[int, int]]]
# ── 3.11 ─────────────────────────────────────────────────────────
# typing.Self — self-reference in class methods
from typing import Self
class Node:
def clone(self) -> Self: ...
# math.cbrt(x) — cube root
math.cbrt(27) # 3.0
# math.exp2(x) — 2^x as float
math.exp2(10) # 1024.0
# tomllib — TOML parsing (not DSA relevant)
# Exception groups and except* (not typically used in DSA)
# Performance: Python 3.11 is ~25–60% faster than 3.10 overall
Quick Version Reference
| Feature | Version | DSA relevance |
|---|---|---|
list[int] in hints | 3.9+ | High — cleaner signatures |
dict | merge | 3.9+ | Medium — merge small dicts |
str.removeprefix/suffix | 3.9+ | Medium — string manipulation |
math.lcm(*args) | 3.9+ | High — number theory |
@cache | 3.9+ | Critical — memoization |
X | Y union hints | 3.10+ | High — cleaner Optional |
match/case | 3.10+ | Medium — state machines |
zip(strict=True) | 3.10+ | Low — safety check |
int.bit_count() | 3.10+ | High — bitmask problems |
Counter.total() | 3.10+ | Medium — frequency sums |
bisect key param | 3.10+ | High — binary search on objects |
typing.Self | 3.11+ | Medium — node class hints |
math.cbrt/exp2 | 3.11+ | Low — rarely needed |
| ~60% speed boost | 3.11 | All problems run faster |