Lists may contain an arbitrary number of elements. There is no way to individually match all of the elements in a list in a case.

You can match all of the elements and then put a guard on the case.

match mylist:
  case [*all_elements] if 'hi' in all_elements:
    ...

This doesn't seem much better than:

if 'hi' in mylist:
  ...

But let's say you want to determine if list has 'hi' as the first element and includes it again?

match mylist:
  case ['hi', *other_elements] if 'hi' in other_elements:
    ...
Answer from Chris on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-match-case-statement
Python Match Case Statement - GeeksforGeeks
December 11, 2025 - The second case matches if the list has exactly three elements, binding them to x, y and z. If the list does not match either pattern, the wildcard _ is used to print "Unknown data format". A mapping is another common data type in Python and match-case can be used to match against dictionaries, ...
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
Sample entrees might be.g Focaccia $16, Aranchini $17. def order_cost(order, prices): match order: case {"pizza": type , "amount": amount}: print(f"{amount} {type} pizza ${amount * costs[type]}") case {"mains": type , "amount": amount}: print(f"{amount} {type} ${amount * costs[type]}") prices ...
🌐
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 ...
🌐
LearnPython.com
learnpython.com › blog › python-match-case-statement
How to Use a match case Statement in Python 3.10 | LearnPython.com
Additionally, the second case has an if statement that matches only when the optional flag --ask is in the input. Below this, you could implement code to accept user input, then delete the files if the command is confirmed. Notice we had to select all the files to delete by using a list comprehension, which is a compact way of writing a for loop. Take a look at this article for more information on for loops in Python. The third case in the above example is matched when the optional flag is not in the input command.
🌐
Datamentor
datamentor.io › python › match-case
Python match...case Statement (With Examples)
Python also lets us use or statements in the cases. For example, number = 2 match number: case 2 | 3: print('Small') case 4 | 5 | 6: print('Medium') case 9 | 10: print('Large') # Output: Small
🌐
Tutorialspoint
tutorialspoint.com › home › python › python match case statement
Python Match Case Statement
February 21, 2009 - Normally Python matches an expression against literal cases. However, it allows you to include if statement in the case clause for conditional computation of match variable. In the following example, the function argument is a list of amount and duration, and the intereset is to be calculated for amount less than or more than 10000.
🌐
Gui Commits
guicommits.com › python-match-case-examples
Python Match Case Examples 🐍🕹️
September 7, 2022 - Let's match specific status codes with the or statement by using |: from http import HTTPStatus import random http_status = random.choice(list(HTTPStatus)) match http_status: case 200 | 201 | 204 as status: # 👆 Using "as status" extracts ...
Find elsewhere
🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
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")
🌐
Programiz
programiz.com › python-programming › match-case
Python match…case Statement
If the status value matches any of the values in a certain case, the code within that case will execute: 200 | 201 | 202: Prints "Success" if status is 200, 201, or 202. 400 | 401 | 403: Prints "Client error" if status is 400, 401, or 403. 500 | 501 | 502: Prints "Server error" if status is ...
🌐
The Teclado Blog
blog.teclado.com › python-match-case
Using "match...case" in Python 3.10
October 26, 2022 - So our first case does not match due to the length of the case expression, even though the comparison expression matches with the first element in the list. The second case is ["hello", name]. This is the case that our input matches with. If you don’t give a literal value for Python to match with, it will bind whatever value there is in the comparison expression to the variable name in the case expression. So in our example, name would be set to George.
🌐
Sling Academy
slingacademy.com › article › python-match-case-statement
Python match/case statement (with examples) - Sling Academy
The as operator is used to bind a variable to the matched value, which can be used inside the case block. The _ operator is used as a wildcard to handle any other value. Next Article: Python: Return Multiple Results from a Function (3 Ways) Previous Article: Making use of the “with” statement in Python (4 examples...
🌐
datagy
datagy.io › home › python posts › python switch (match-case) statements: complete guide
Python Switch (Match-Case) Statements: Complete Guide • datagy
February 23, 2022 - This means that it doesn’t matter how many items are in that list: # Matching a complex structure values = ['John', 'Matt', 'Kate', 'Nik', 'Evan'] # Returns: # More than three names: John, Matt, Kate, as well as: Nik, Evan · Python match-case statements can also be used to check the types of something being passed in. Let’s move this to a more advanced example and create a function that let’s us pass in different values.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to use structural pattern matching in Python | InfoWorld
August 9, 2023 - Some examples: ... case ["a", value1]: Match against a collection with two values, and place the second value in the capture variable value1. case ["a", *values]: Match against a collection with at least one value.
🌐
Analytics Vidhya
analyticsvidhya.com › home › what is match case statement in python?
What is Match Case Statement in Python? - Analytics Vidhya
May 29, 2024 - In this example, we define a function process_data that takes a list as input. The match case statement matches the list’s value against different patterns, including patterns with multiple elements.
🌐
Towards Data Science
towardsdatascience.com › home › latest › the match-case in python 3.10 is not that simple
The Match-Case In Python 3.10 Is Not That Simple | Towards Data Science
January 21, 2025 - Let’s see the example below. def http_status(status): match status: case 400: return "Bad request" case 401: return "Unauthorized" case 403: return "Forbidden" case 404: return "Not found" case _: return "Other error" In the code above, we ...
Top answer
1 of 2
73

Rather than match type(v), match v directly:

values = [
    1,
    "hello",
    True,
]

for v in values:
    match v:
        case str():
            print("It is a string!")
        case bool():
            print("It is a boolean!")
        case int():
            print("It is an integer!")
        case _:
            print(f"It is a {type(v)}!")

Note that I've swapped the order of bool() and int() here, so that True being an instance of int doesn't cause issues.

This is a class pattern match.

2 of 2
11

You can match directly against the type of v, but you need a value pattern to refer to the types to match, as a "dotless" name is a capture pattern that matches any value. For example,

import builtins


values = [
    1,
    "hello",
    True
]

# Caveat: this will continue to work even if someone
# rebinds the built-in, but not, for example, if builtins.str
# itself is rebound.
for v in values:
    match type(v):
        case builtins.str:
            print("It is a string!")
        case builtins.int:
            print("It is an integer!")
        case builtins.bool:
            print("It is a boolean!")
        case _:
            print(f"It is a {type(v)}!")

Note that a value pattern must be a dotted name; it's not an arbitrary expression that can evaluate to a specific value.

(Whether you really want to match against the actual type of a value, or really want to determine if a value is an instance of a given type, is another matter. In the latter case, an if-elif statement is needed.

if isinstance(v, bool):
    print("It is a boolean!")
elif isinstance(v, int):
    print("It is an int!")
elif isinstance(v, str):
    print("It is a string!")
else:
    print(f"It is a {type(v)}!")

There is no pattern that lets you use the result of calling isinstance as the case to match against. )