In Python 3.10 and later versions, the match statement was introduced as an enhancement to the switch statement. The match statement allows you to compare a value against multiple patterns and execute code based on the matched pattern. Here’s how you can use match with multiple values:
def process_data(value):
match value:
case 1 | 2:
print("Value is 1 or 2")
case 3:
print("Value is 3")
case _:
print("Value is something else")
process_data(1)
process_data(2)
process_data(3)
process_data(4)
In the above example, the match statement checks the value against different patterns using the case keyword. The | operator is used to combine multiple values in a single pattern. If the value matches any of the specified patterns, the corresponding block of code is executed. The _ pattern acts as a catch-all for any values that haven’t been matched by the previous patterns.
Python match case multiple values example
Here’s a more comprehensive example of using the match statement in Python.
def process_data(value):
match value:
case 1 | 2:
print("Value is 1 or 2")
case 3:
print("Value is 3")
case 4 | 5 | 6:
print("Value is 4, 5, or 6")
case _:
print("Value is something else")
process_data(1)
process_data(2)
process_data(3)
process_data(4)
process_data(5)
process_data(6)
process_data(7)
Output:

This code defines a function process_data that takes an argument value. The function uses the new match statement introduced in Python 3.10 to determine the value of the input and execute specific code based on the matching patterns. Let’s break down the code step by step:
- The
process_datafunction is defined, which takes a single parametervalue. - The
matchstatement is used to compare thevalueagainst different patterns using thecasekeyword. case 1 | 2:: This line matches values that are either 1 or 2. If thevalueis 1 or 2, the associated block of code is executed, printing “Value is 1 or 2”.case 3:: This line matches the value 3. If thevalueis 3, the associated block of code is executed, printing “Value is 3”.case 4 | 5 | 6:: This line matches values that are either 4, 5, or 6. If thevalueis any of these values, the associated block of code is executed, printing “Value is 4, 5, or 6”.case _:, where_is a wildcard pattern: This is a catch-all case that matches any value that did not match the previous patterns. If thevaluedoes not match any of the previous cases, this block of code is executed, printing “Value is something else”.- The code then calls the
process_datafunction multiple times with different values: 1, 2, 3, 4, 5, 6, and 7. Depending on the input value, the function will print one of the specified messages.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.