Python's match-case statement, called structural pattern matching, compares a value against a series of patterns and runs the block of the first pattern that fits. Unlike a C-style switch, a pattern can check the shape of the data (a tuple of two items, a dict with a given key, an object of a given class) and bind parts of it to variables in the same step.
Structural pattern matching was added in Python 3.10 (PEP 634). This guide covers the pattern types you will actually use, guards, and the pitfalls, with examples for Python 3.13 and 3.14.
A match statement takes a subject expression and tries each case from top to bottom. The first one that matches runs, and no other case runs afterward (there is no fall-through and no break).
The underscore _ is the wildcard that matches anything, so it works as a default. If no case matches and there is no wildcard, nothing happens and no error is raised. Use | to match several values in one case: case 401 | 403:.
A bare name in a pattern is a capture: it matches anything and binds the value to that name.
This means you cannot put a plain variable in a pattern to compare against it. case RED: does not compare to a constant named RED; it captures the subject into a new variable named RED and always matches. To compare against a constant, use a dotted name such as Color.RED, which Python treats as a value pattern.
Lists and tuples are matched by length and content. A starred name captures the rest.
Sequence patterns match any sequence except str, bytes and bytearray, so a string is never treated as a list of characters here.
A mapping pattern checks that the keys exist and matches the values. Extra keys are ignored unless you prohibit them.
This is especially useful for parsed JSON, where you branch on the structure of a dict.
A class pattern checks the type with isinstance and then matches attributes.
Positional patterns like Point(x, y) work for dataclasses and named tuples because they define __match_args__. For other classes, use keyword patterns.
Add if after a pattern to run an extra test. The case matches only if both the pattern and the guard succeed.
Use as to name a sub-pattern: case [Point() as first, *_]:. Combine alternatives with |, but every alternative must bind the same names.
case _: or case x: before specific ones. Python raises a SyntaxError for an irrefutable pattern that is not last, but a broad class or sequence pattern can still shadow later cases silently.match is a soft keyword. It is only special at the start of a statement, so a variable named match still works.case _: that raises if you want one.Python 3.10 and later. It is not available in 3.9 or earlier.
No. A switch compares a value against constants, while match destructures sequences, mappings and objects and binds variables.
No. Only the first matching case runs.
Yes, with a class pattern such as case int(): or case str() as s:.
match-case tests a subject against patterns from top to bottom and runs the first match. It supports literals, captures, sequences, mappings and class patterns, plus |, as and if guards. Remember that bare names capture, use dotted names for constants, and finish with case _: when you need a default.