ReviseAlgo Logo

Control Flow

Match-Case (Python 3.10+)

Pattern matching in Python

Interview: Modern Python feature — tests knowledge of structural pattern matching beyond simple switch-case

Last Updated: June 12, 2026 12 min read

Introduced in Python 3.10 via PEP 634, match-case provides structural pattern matching — far more powerful than a simple switch-case. It can match on values, types, structures (lists, tuples, dicts), and even class attributes, with optional guard conditions.

Basic Value Matching

  • Literal patterns: Match specific values (numbers, strings, booleans, None)
  • Wildcard pattern: case _: matches anything (like default in switch). Must be last
  • OR patterns: case "yes" | "y": matches multiple values
  • No fall-through: Unlike C switch, only the first matching case executes

Structural Patterns

  • Sequence patterns: case [x, y]: matches a 2-element list/tuple and binds values
  • Star pattern: case [first, *rest]: captures remaining elements (like *args)
  • Mapping patterns: case {"name": name, "age": age}: matches dict keys
  • Class patterns: case Point(x=0, y=0): matches instances and extracts attributes

Guard Clauses

Add if conditions to cases for additional filtering beyond structural matching:

  • case (x, y) if x == y: — matches tuples where both values are equal
  • case [x, *rest] if len(rest) > 5: — structural match + length guard
  • Guards are evaluated after the pattern matches; if the guard is False, the next case is tried

Key Difference from Switch-Case

Match-case is NOT just a switch statement. It performs structural decomposition — it can simultaneously check the shape of data AND extract components. A single match-case can replace nested if-elif chains that check types, lengths, and values.

Important Caveats

  • Capture vs constant: Bare names like case x: capture the value into variable x (they don't compare). Use dotted names for constants: case Status.OK:
  • Requires Python 3.10+: Not available in earlier versions
  • match and case are soft keywords: They only have special meaning in match statements; can still be used as variable names elsewhere
  • Exhaustiveness: Always include a case _: wildcard or be sure all patterns are covered

Best Practice

Use match-case when you're dispatching on the structure of data (different shapes/types). For simple value dispatch (enum-like), a dictionary lookup is often cleaner and works in all Python versions.

Use Cases

Parsing structured data (JSON, commands, protocols)

Replacing complex if-elif chains that check types and structure

State machine implementations

Event/message dispatching based on message shape

Common Mistakes

Using bare names thinking they are constants: case x: captures ANY value into x (use dotted names for enum constants)

Forgetting the wildcard case _: — unmatched values silently do nothing

Trying to use match-case in Python < 3.10 (it is a syntax error)

Using match-case for simple value dispatch where a dict lookup would be cleaner and more compatible