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, ...
Discussions

python - How to check if list includes an element using match case? - Stack Overflow
I'm trying to check if a single element is in a list using match case. I'm not very familiar with these new keywords so 90% sure I'm using them wrong. Regardless, is there a way to do this? This is... More on stackoverflow.com
🌐 stackoverflow.com
Proposal: `for in match` within list comprehension - Ideas - Discussions on Python.org
Just for discussion (I’m new to Python by the way). I saw an example for pattern matching with Class in Fluent Python (2nd Edition): class City(typing.NamedTuple): continent: str name: str country: str citi… More on discuss.python.org
🌐 discuss.python.org
0
April 14, 2022
pattern matching - Using Python's match/case on repeated elements in a list - Stack Overflow
I want to use Python's match/case to detect when there's a repeated element in a (short) list, so that if the list is of the form [n, n], then I can do some arithmetic on n in the case statement. H... More on stackoverflow.com
🌐 stackoverflow.com
Anyone used match case yet?
Last I used match/case, I used it as a replacement to isinstance. match/case + dataclasses + "mypy static typing" work together to help me get exhaustive type-casing and destructuring. More on reddit.com
🌐 r/Python
82
80
January 27, 2024
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
However, as the rationale PEP points out, it’s better thought of as a “generalized concept of iterable unpacking”. Many people have asked for a switch in Python over the years, though I can see why that has never been added. It just doesn’t provide enough value over a bunch of if ... elif statements to pay for itself. The new match ... case feature provides the basics of switch, plus the “structural” matching part – and some.
🌐
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")
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
In executing do_action("use sword", weapons), the first case statement will check to see if the weapon, sword, is in the weapons list. The second action with the crossbow will result in the print statement indicating that the weapon can’t be used. def do_action(action, weapons): match action.split(): case ["use", weapon] if weapon in weapons: print(f"using {weapon}") case ["use", _]: print(f"Can't use that weapon.") weapons = ["dagger", "sword", "spear"] do_action("use sword", weapons) do_action("use crossbow", weapons)
🌐
Medium
lynn-kwong.medium.com › clean-up-your-if-else-python-match-case-explained-for-beginners-3aa29b442091
Clean Up Your if/else: Python match/case Explained for Beginners | by Lynn G. Kwong | Nov, 2025 | Medium | Medium
November 29, 2025 - In simple uses, you can match literal constants just like a switch. For example, you can write a http_error() function using match status: case 400: … case 404: … case _: … to handle HTTP codes with a default fall-back.
Find elsewhere
🌐
Gui Commits
guicommits.com › python-match-case-examples
Python Match Case Examples 🐍🕹️
September 7, 2022 - As I mentioned initially, the match case goes beyond a regular switch case. 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 its value print(f"Everything is good!
🌐
Python.org
discuss.python.org › ideas
Proposal: `for in match` within list comprehension - Ideas - Discussions on Python.org
April 14, 2022 - I saw an example for pattern matching ... class City(typing.NamedTuple): continent: str name: str country: str cities = [ City('Asia', 'Tokyo', 'JP'), City('Asia', 'Delhi', 'IN'), City('North America', 'Mexico City', 'MX'), City('North America', 'New York', 'US'), City('South America', 'São Paulo', 'BR'), ] def match_brazil(): results = [] for city in cities: ...
🌐
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
🌐
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.
🌐
Medium
medium.com › @muhammadshafey063 › python-match-cases-and-their-types-with-uses-0cf2f54cc730
Python: Match cases and their types with uses | by Muhammad shafey | Medium
June 1, 2024 - With its various types, such as Simple Match, Tuple Match, List Match, Dictionary Match, Wildcard Match, and Guard Clause, Match Case provides a robust way to match values against different patterns. By using Match Case, developers can write more efficient, readable, and maintainable code. It is an essential feature in Python that makes coding easier and more enjoyable.
🌐
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 ...
🌐
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
🌐
LearnPython.com
learnpython.com › blog › python-match-case-statement
How to Use a match case Statement in Python 3.10 | LearnPython.com
May 9, 2022 - 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...
🌐
Udacity
udacity.com › blog › 2021 › 10 › python-match-case-statement-example-alternatives.html
Python Match-Case Statement: Example & Alternatives | Udacity
September 27, 2022 - It’s sometimes challenging, but ... is the match-case statement. Introduced in Python 3.10, it allows you to evaluate an expression against a list of values....
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to use structural pattern matching in Python | InfoWorld
September 8, 2023 - In fact, the chief use case for structural pattern matching is to match patterns of types, rather than patterns of values. Python performs matches by going through the list of cases from top to bottom. On the first match, Python executes the ...
🌐
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 - If it doesn’t match one of the given 3 "times", the default case will be executed. Why there is a question mark there? I intentionally added it because I want to emphasise the solution. Generally, it won’t be easy to match one of the sub-patterns and then reference which pattern it matched exactly. However, we can have this "reference" in Python.
🌐
datagy
datagy.io › home › python posts › python switch (match-case) statements: complete guide
Python Switch (Match-Case) Statements: Complete Guide • datagy
February 23, 2022 - Let’s take a look at the case where more than three items are included. We use the * to unpack any items following the first. 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.