Not as far as I know in python. In this case I wouldn't use pattern matching at all:

puts = {
    "slow": "v2 k5",
    "balanced": "v3 k5",
    "fast": "v3 k7"
}.get(Mode, default)

Clearer, more declarative, and no repeating yourself. But of course you can't use clever pattern destructuring.

Answer from 2e0byo on Stack Overflow
🌐
Python.org
discuss.python.org › ideas
Feature Suggestion - Assign match case result to a variable - Ideas - Discussions on Python.org
August 7, 2023 - Feature or enhancement Assign ... and execute code blocks based on patterns. However, the result of the match-case statement cannot be directly assigned to a variable....
Discussions

Opinions on match-case?
Match-case is great for structural pattern matching (which is what it's designed for) but you cannot just replace any and all if statements with match-case. So while the match-case might be more readable in itself, if you blindly go and switch if-else statements to match-case where you can, your code as a whole will become less readable because it keeps jumping from one control flow method to another. So I use match-case in specific cases where I need to match some specific structural pattern, but in most cases I feel it's better to stick with your regular if-else. More on reddit.com
🌐 r/Python
62
16
February 12, 2025
"Guarded" Match/Case statements
My issue was, what if I want to match something in the dictionary against a variable You can't do it with a plain variable, but its worth noting that you can if its a a qualified (dotted) name, which may or may not be an option depending on your situation. Ie. you can't do (or at least, it won't do what is intended): Red = 0xFF0000 match x: case Red: And would need to usage a guard like you describe (case _ if x == Red:): But you can do: class Colour: Red = 0xFF0000 ... match x: case Colour.Red: More on reddit.com
🌐 r/learnpython
12
35
June 8, 2022
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
In addition, the empty parentheses in the various cases are a bit weird – they look unnecessary with no positional arguments or attributes, but without them match would bind new variables named str or dict. At first I thought it was weird how the variables bound (and assigned) in a case block ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-match-case-statement
Python Match Case Statement - GeeksforGeeks
December 11, 2025 - In this example, the function check_number(x) uses a match-case statement to compare the value of x to the constants 10 and 20. If x equals 10, it prints "It's 10". If x equals 20, it prints "It's 20". If neither condition is met, the wildcard _ matches any value, leading to the message "It's neither 10 nor 20". Let's take a look at python match case statement in detail:
🌐
Python Morsels
pythonmorsels.com › match-case-parsing-python
Appreciating Python's match-case by parsing Python code - Python Morsels
June 22, 2022 - That match statement is very complex, but it's much less visually dense than that if statement was. That second case statement ensures that the annotated assignment node we're matching has a value attribute which is either field(...) or dataclasses.field(...).
🌐
Tutorialspoint
tutorialspoint.com › home › python › python match case statement
Python Match Case Statement
February 21, 2009 - A Python match-case statement takes an expression and compares its value to successive patterns given as one or more case blocks. Only the first pattern that matches gets executed.
🌐
Reddit
reddit.com › r/python › opinions on match-case?
r/Python on Reddit: Opinions on match-case?
February 12, 2025 -

I am curious on the Python community’s opinions on the match-case structure. I know that it has been around for a couple of years now, but some people still consider it relatively new.

I personally really like it. It is much cleaner and concise compared if-elif-else chains, and I appreciate the pattern-matching.

match-case example:

# note that this is just an example, it would be more fit in a case where there is more complex logic with side-effects

from random import randint

value = randint(0, 2)

match value:
    case 0:
        print("Foo")
    case 1:
        print("Bar")
    case 2:
        print("Baz")
Find elsewhere
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
Match case can be used to replace lengthy if-elif blocks, making the alternatives more readable. An example of a simple if-elif block is below. age_flag = False if age_flag == True: print("Entry permitted") elif age_flag == False: print("No entry until you reach 13 years of age.") In the simple pattern below, the subject is checked against each case pattern, in order, till a match is found.
🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
The fourth pattern captures two values, which makes it conceptually similar to the unpacking assignment (x, y) = point. If you are using classes to structure your data you can use the class name followed by an argument list resembling a constructor, but with the ability to capture attributes into variables: from dataclasses import dataclass @dataclass class Point: x: int y: int def where_is(point): match point: case Point(x=0, y=0): print("Origin") case Point(x=0, y=y): print(f"Y={y}") case Point(x=x, y=0): print(f"X={x}") case Point(): print("Somewhere else") case _: print("Not a point")
Top answer
1 of 1
1

In the pattern ["make", cmd], cmd is a capture pattern. Capture patterns (which look like un-dotted names such as foo, bar, or cmd) match anything, and unconditionally bind the matched value to that name.

In your example, any sequence of length 2 starting with "make" will bind the second element to cmd and execute the second case block. This is useful since it allows your app to easily determine which make command it should execute.

This happens whether cmd is already assigned or not. So, if it is already bound, that old value will be overwritten with the new, matched value.

If you want to compare a name by value instead, you have a couple of options depending on your situation:

  • If the name is dotted (like args.cmd or Class.VAR), it will automatically compare by equality (and not capture). So you can just use ["make", args.cmd] as your pattern.

  • If the name is bare (like cmd or VAR), you can use a capture pattern together with a guard to compare by equality. Using this technique, your pattern could look like ["make", c] if c == cmd. Note that since c is a capture pattern, it will be rebound just like cmd in your original example! (As you use pattern matching more, you'll find that capture patterns like c are extremely useful when performing more complex pattern matches.)

I recommend that anybody looking to familiarize themselves with Python's structural pattern matching check out the official tutorial. It covers all of the different patterns (including capture patterns) in a very easy-to-understand way.

🌐
Python
peps.python.org › pep-0622
PEP 622 – Structural Pattern Matching | peps.python.org
Like unpacking assignment, both tuple-like and list-like syntax can be used, with identical semantics. Each element can be an arbitrary pattern; there may also be at most one *name pattern to catch all remaining items: match collection: case 1, [x, *others]: print("Got 1 and a nested sequence") case (1, x): print(f"Got 1 and {x}")
🌐
Udacity
udacity.com › blog › 2021 › 10 › python-match-case-statement-example-alternatives.html
Python Match-Case Statement: Example & Alternatives | Udacity
September 27, 2022 - If the CPU model the user enters doesn’t match our expected options, the final case statement prints an error message. Here’s a possible output: Please enter your CPU model: core i9 Our teams designed nice loading screens... Too bad you won't see them... The program worked as expected and printed out the corresponding statement after the player introduced their CPU model. Python 3.10 is gradually being rolled out in 2021.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to use structural pattern matching in Python | InfoWorld
September 8, 2023 - Python performs matches by going through the list of cases from top to bottom. On the first match, Python executes the statements in the corresponding case block, then skips to the end of the match block and continues with the rest of the program.
🌐
Medium
techytales.medium.com › pythons-switch-statement-a-beginner-s-guide-4aaffef29cb5
Python’s Alternative for Switch Statement: A Beginner’s Guide for match case statement | by Nihal Patel | Medium
June 21, 2023 - The fourth case statement uses a sequence pattern (x, y) to extract both values from the tuple and assign them to variables. If this pattern matches, the output will be X=2, Y=3. The last case statement uses the wildcard pattern _ to catch any values that don’t match any of the previous patterns. If this pattern matches, a ValueError is raised. The patterns in a match-case statement can be any valid Python expression.
🌐
Wrighters
wrighters.io › home › a gentle introduction to the python match statement
A Gentle Introduction to the Python Match Statement - wrighters.io
April 2, 2023 - What it is saying is that you have ... to match the same thing: a case where the length of the values list can be assigned to a single variable. This is not checking the value of the length of values. There is a way to do that, however, if you needed to. You have to use dotted notation so that ...
🌐
Earthly
earthly.dev › blog › structural-pattern-matching-python
Structural Pattern Matching in Python - Earthly Blog
July 19, 2023 - This tutorial uses the Python Requests library to retrieve fake blog post data from the JSONPlaceholder API. Therefore, a working knowledge of the library will be helpful but not required. You may install the Requests library using the pip package manager: ... match <expression>: case <pattern 1> [<if guard>]: <block to execute if pattern 1 matches> case <pattern n> [<if guard>]: <code block to execute if pattern n matches>
🌐
Stackademic
blog.stackademic.com › python-match-case-statement-63d01477e1c0
Python Match-Case Statement. Starting from Python 3.10, the Python… | by Caner Uysal | Stackademic
June 12, 2024 - The match statement allows you to perform different actions based on a set of patterns that match the values or data structures of variables. The match statement uses case statements to specify the cases where data structures or patterns match.
🌐
Anthony Shaw
tonybaloney.github.io › posts › python-match-statement.html
Using the Python match statement
With the match statement, cases are evaluated in order. Use this to set specific cases at the top and then get more generic as you go down.
🌐
Reddit
reddit.com › r/learnpython › "guarded" match/case statements
r/learnpython on Reddit: "Guarded" Match/Case statements
June 8, 2022 -

I was doing some research into match/case statements trying to figure out how to compare the match value to a variable as opposed to a static entry and found a seeming lack of information on the topic. I was about to post here for help but ended up finding some info that helped me, so I figured I'd post here so that it might help others and that people can discuss it if they found it helpful.

If you've seen switch/case statements in other languages or started looking at the relatively new match/case in Python you are probably familiar with something like this:

BASIC MATCH/CASE

x = 1

match x:
    case 1:
        print("Number 1")
    case 2:
        print("Number 2")
    case str():
        print("It's a string")
    case {"key": "value"}:
        print("Dictionary with Keys and Values")
    case _:
        print("Didn't match anything")

We start matching against the variable X, testing each case from top to bottom. If it's the integer numbers 1 or 2, we print our "Number 1/2". Next we check to see if x is a string by seeing if it matches the built-in string class, then check to see if it a dictionary with a key called "key" and a corresponding value called "value". Finally if we missed everything above, we catch everything else with "_".

You may also know about being able to extract data from your matching variable into independent variables for use.

MATCH/CASE WITH ASSIGNMENTS

x = {"type": "order", "id": 24}

match x:
    case {"type": "order", "id": idnumber}:
        print(f"Order found with id number: #{idnumber}")

For instance, here we check to see if x is a dictionary with a key called "type" with the value set to "order". If so, we match, and we extract the value under the key "id" and store it in a variable called "idnumber". This is used in the fstring print statement, and of course we could use this in more complicated logic. Note that the value of "id" can be anything, but it MUST be present. If "id" is absent, we won't match this case at all.

My issue was, what if I want to match something in the dictionary against a variable, instead of assinging it to a variable. For instance, what if we have an order date and we want to reference it against today's date, which is calculated dynamically.

It turns out you can use an "if" statement along with the case statement, all on the same line. This makes it look a little cleaner than having your if statement nested inside the case statement, and you don't have an extra indent for your block of code.

GUARDED MATCH/CASE WITH ASSIGNMENTS

x = {"type": "order", "id": 24, "date": 123456}
todays_date = 123458

match x:
    case {"type": "order", "id": idnumber, "date": orderdate} if orderdate >= todays_date-30:
        print(f"Order found with id number: #{idnumber} and date {orderdate}")
    case {"type": "order", "id": idnumber, "date": orderdate} if orderdate < todays_date-30:
        print(f"Aging order found with id number: #{idnumber} and date {orderdate}")
    case _:
        print("Order not found")

Here we are checking to make sure that we have a dictionary with "type' set to "order", some "id" which we extract to the variable idnumber, and some "date" which we extract to orderdate. We then do a check to make sure that the order date is greater or equal to today's date minus 30. If so we print that data out. If the order is older, we match the second case. If we don't match either, we catch it with the default.

DEFAULT GUARDED MATCH/CASE

x = 5
y = 6
z = 5

match x:
    case _ if x == y:
        print("x equals y")
    case _ if x == z:
        print("x equals z")

We can even do something like this to entirely drive the case off an if statement. Of course we may want to debate if we should do this vs simply just using an if instead of a match/case; for something this simplistic a regular if conditional would probably be better but this demonstrates an ability that might be useful in another program.

And just as a final bonus, don't forget can even do complex matches looking inside classes with something like:

UNREASONABLE BUT FUNCTIONAL MATCH/CASE WITH CLASSES AND LAMBDAS BECAUSE WE CAN

class class1:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

l = [1, 2]
c1 = class1(1, 2, 3)

match c1:
    #The first equal sign here is actually an equality not an assignment!
    case class1(a=1, b=x, c=y) if x == next(filter((lambda q: q % 2 == 0), l)):
        print(f"Matched Class1 with values {x} {y}")

Don't ever actually do this, it's clearly Rube Goldberg code, and everyone will hate you if you do it. But it's mostly just here to prove that you can incorporate all sorts of crazy elements if you end up needing any... just think about if you should before you do.

🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
month = 5 day = 4 match day: case 1 | 2 | 3 | 4 | 5 if month == 4: print("A weekday in April") case 1 | 2 | 3 | 4 | 5 if month == 5: print("A weekday in May") case _: print("No match") Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial