Vasu Patel logoVasu Patel
← Python Syntax & DSA Reference
All chapters in this track
  1. 011. Variables, Types & Operators
  2. 022. Strings
  3. 033. Lists
  4. 044. Tuples, Sets & Dicts
  5. 055. Control Flow
  6. 066. Error Handling
  7. 077. Functions & Lambdas
  8. 088. Comprehensions
  9. 099. Built-in Functions
  10. 1010. Type Hints (3.10+)
  11. 1111. Classes & Dataclasses
  12. 1212. Iterators & Generators
  13. 1313. DSA Standard Library
  14. 1414. Python 3.9–3.11 Features for DSA
  15. 1515. DSA Idioms & Gotchas
  16. 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

FeatureVersionDSA relevance
list[int] in hints3.9+High — cleaner signatures
dict | merge3.9+Medium — merge small dicts
str.removeprefix/suffix3.9+Medium — string manipulation
math.lcm(*args)3.9+High — number theory
@cache3.9+Critical — memoization
X | Y union hints3.10+High — cleaner Optional
match/case3.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 param3.10+High — binary search on objects
typing.Self3.11+Medium — node class hints
math.cbrt/exp23.11+Low — rarely needed
~60% speed boost3.11All problems run faster