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, checking for specific keys and values.
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
Python match-case statements can be used to check the types of something being passed in. In the code below, lists [ ], tuples ( ) and sets { } are distinguished.
🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
Instead of writing many if..else statements, you can use the match statement. The match statement selects one of many code blocks to be executed. match expression: case x: code block case y: code block case z: code block
🌐
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...
🌐
Reddit
reddit.com › r/learnpython › looping inside match-case?
r/learnpython on Reddit: Looping inside match-case?
October 21, 2021 -

I’ve posted this on stackoverflow as well but I wanted to ask here as well.

Does looping within a match-case not work? I can loop outside of the match-case as many times as I want but once inside the case, it only iterates once.

Does this not work and is there a reason for doing so, or a way to accomplish this?

Thanks!

Link to stackoverflow

edit: I found out my issue comes from a try statement at the bottom of a case that wasn’t commented out. Did that and it fixed the whole problem. I feel so dumb.

🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
Note that, in a similar way to unpacking assignments, you can use either parenthesis, brackets, or just comma separation as synonyms. So you could write case action, obj or case (action, obj) with the same meaning. All forms will match any sequence (for example lists or tuples).
Find elsewhere
🌐
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 ... 'New York', 'US'), City('South America', 'São Paulo', 'BR'), ] def match_brazil(): results = [] for city in cities: ......
🌐
Datamentor
datamentor.io › python › match-case
Python match...case Statement (With Examples)
Now, the value of plot1 is compared with each case statement. Since the value matches with case (0,4), the statement inside it is executed. To learn more about tuples, visit Python Tuple. Python also lets us use or statements in the cases. For example,
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
For this last case, many people use the ellipsis literal ... instead of pass. This use has no special meaning to Python, and is not part of the language definition (you could use any constant expression here), but ... is used conventionally as a placeholder body as well. See The Ellipsis Object. A match statement takes an expression and compares its value to successive patterns given as one or more case blocks.
🌐
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.
🌐
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.
🌐
Geek Python
geekpython.in › match-case-in-python
How To Use Match Case Statement For Pattern Matching In Python
August 15, 2023 - The above code will run a for loop for every letter in the seq variable and tries to match i which is basically every letter in the seq, we created three case blocks, the first one matches if seq has the letter "y" in it, so does the second one but for the letter "e" and the third one matches the other letters. Here we’ll see how we can create the match-case statement using the Python ...
🌐
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!
🌐
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 necessary, to stay up-to-date on the latest innovations and features. One of the language’s most recent additions is the match-case statement. Introduced in Python 3.10, it allows you to evaluate an expression against a list of values.
🌐
Programiz
programiz.com › python-programming › match-case
Python match…case Statement
If the status value matches any ... 403. 500 | 501 | 502: Prints "Server error" if status is 500, 501, or 502. In Python, we can also use the if statement in the case clauses....