Python Pattern Matching (match/case) Cheat Sheet
Structural pattern matching syntax introduced in Python 3.10: literal, capture, sequence, mapping, class patterns, and guards.
1 PageIntermediateFeb 18, 2026
Basic match/case
Like switch, but matches structure, not just equality.
python
def http_status(code): match code: case 200: return "OK" case 404: return "Not Found" case 500 | 502 | 503: # OR pattern return "Server Error" case _: # wildcard, catches everything else return "Unknown"
Sequence & Capture Patterns
Destructure lists/tuples, bind names, and use * for the rest.
python
def describe(point): match point: case (0, 0): return "origin" case (0, y): return f"on y-axis at {y}" case (x, 0): return f"on x-axis at {x}" case (x, y): return f"point at ({x}, {y})" case [first, *rest]: return f"list starting with {first}, then {rest}" case _: return "not a point"
Mapping & Class Patterns
Match dict keys or destructure objects by attribute.
python
def handle(event): match event: case {"type": "click", "x": x, "y": y}: return f"click at {x},{y}" case {"type": "key", **rest}: # **rest captures remaining keys return f"key event: {rest}" case _: return "unhandled"from dataclasses import dataclass@dataclassclass Point: x: int y: intdef classify(p): match p: case Point(x=0, y=0): return "origin" case Point(x=0, y=y): return f"y-axis: {y}" case Point() as pt: # capture whole match with 'as' return f"somewhere: {pt}"
Guards & Nested Patterns
Add an if condition to a case, and nest patterns arbitrarily.
python
def categorize(value): match value: case int(n) if n < 0: return "negative int" case int(n) if n == 0: return "zero" case int(n): return "positive int" case [int(a), int(b)] if a == b: return "pair of equal ints" case str() as s if s.isupper(): return "shouty string" case _: return "other"
Pattern Types Reference
The building blocks that combine into a case pattern.
- literal pattern- case 42, case "foo", case None, case True
- capture pattern- case x binds the whole subject to name x
- wildcard pattern- case _ matches anything, binds nothing
- OR pattern- case 1 | 2 | 3 matches any of the alternatives
- sequence pattern- case [a, b, *rest] destructures list/tuple-like objects
- mapping pattern- case {"key": val} destructures dict-like objects
- class pattern- case ClassName(attr=val) matches type and attributes
- guard- case pattern if condition adds an extra boolean check
Pro Tip
A bare name in a case pattern is always a capture (binds a new variable), never an equality check against an existing variable — to match against an existing variable's value use a dotted name or a guard, e.g. case x if x == existing_var, or case SomeEnum.VALUE for enum members.
Was this cheat sheet helpful?
Explore Topics
#PythonPatternMatchingMatchCase#PythonPatternMatchingMatchCaseCheatSheet#Programming#Intermediate#BasicMatchCase#SequenceCapturePatterns#MappingClassPatterns#GuardsNestedPatterns#OOP#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance