Python

Python's Match Statement: Pattern Matching That Actually Feels Natural

Learn how Python's match statement from version 3.10 simplifies branching logic with pattern matching on literals, sequences, and dictionaries, plus guards and wildcards.

August 2026 5 min read 12 views 0 hearts

Remember that moment when you first saw a switch statement in another language and thought "why doesn't Python have this?" Well, Python finally caught up in version 3.10 with the match statement, and honestly, it's better than most switch statements I've worked with.

Let me show you what I mean.

The Basics: Starting Simple

Here's what a basic match looks like:

def describe_animal(animal):
    match animal:
        case "dog":
            return "A loyal friend"
        case "cat":
            return "A mysterious companion"
        case "bird":
            return "A sky explorer"
        case _:
            return "Some other creature"

That underscore at the end is our wildcard – it catches everything that doesn't match above. Without it, unmatched inputs just silently do nothing.

Matching Multiple Patterns

Sometimes you want the same outcome for different inputs. No problem:

def check_code(code):
    match code:
        case 200 | 201 | 204:
            return "Success!"
        case 400 | 403 | 404:
            return "Error on your end"
        case 500 | 502 | 503:
            return "Server issue"
        case _:
            return "Unknown code"

The pipe character (|) works like "or" here. Clean and readable.

Pattern Matching with Data Structures

This is where match really shines. Let me show you something you can't do with a plain if-else:

def process_data(data):
    match data:
        case [x, y]:
            return f"Two items: {x} and {y}"
        case [x, y, *rest]:
            return f"First two: {x} and {y}, plus {len(rest)} more"
        case {"name": name, "age": age}:
            return f"My name is {name}, I'm {age}"
        case _:
            return "Unrecognized format"

See how we're matching against list lengths and dictionary keys directly? That's pattern matching at its finest.

Guard Conditions for Extra Control

Sometimes a pattern isn't enough – you need conditions too:

def classify_point(point):
    match point:
        case (x, y) if x == y:
            return "On the diagonal line"
        case (x, y) if x > 0 and y > 0:
            return "First quadrant"
        case (x, y) if x < 0 and y > 0:
            return "Second quadrant"
        case (x, y):
            return "Somewhere else"

The if after the pattern adds a condition. The pattern matches first, then the condition must be true.

A Real-World Example

Here's something you might actually use at PythonSkillset when building content systems:

def process_article_request(request):
    match request:
        case {"type": "create", "title": title, "tags": tags}:
            return create_article(title, tags)
        case {"type": "update", "id": article_id, "changes": changes}:
            return update_article(article_id, changes)
        case {"type": "delete", "id": article_id} if user_is_admin():
            return delete_article(article_id)
        case _:
            return "Invalid request"

No more long chains of if "type" in request checks. The intent is right there.

What Can't It Match?

Match works with: - Literal values (numbers, strings, booleans, None) - Variable names (captures the value) - Sequences (lists, tuples) - Mappings (dictionaries) - Classes and their attributes - Wildcard patterns with _

It doesn't work with arbitrary expressions. So case x + 1: won't compile – you'd need a guard condition for that.

One Thing to Watch Out For

Variable names in patterns are capture patterns, not comparisons. This tripped me up early:

x = 5
match something:
    case x:  # This always matches and binds to x
        print("Matched")

Here x gets rebound to whatever something is. Use a literal or guard to compare against an existing variable:

x = 5
match something:
    case 5:
        print("Matched")
    case val if val == x:
        print("Also matched")

The Bottom Line

The match statement isn't just a fancy switch – it's a whole new way to think about branching logic in Python. Once you start using it for pattern matching on data structures, you'll wonder how you managed without it.

Give it a try in your next PythonSkillset project. Start with simple cases, then work your way up to matching complex data. The readability payoff is worth it.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.