Skip to content

Python match case multiple values

  • by

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:

Python match case multiple values

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:

  1. The process_data function is defined, which takes a single parameter value.
  2. The match statement is used to compare the value against different patterns using the case keyword.
  3. case 1 | 2:: This line matches values that are either 1 or 2. If the value is 1 or 2, the associated block of code is executed, printing “Value is 1 or 2”.
  4. case 3:: This line matches the value 3. If the value is 3, the associated block of code is executed, printing “Value is 3”.
  5. case 4 | 5 | 6:: This line matches values that are either 4, 5, or 6. If the value is any of these values, the associated block of code is executed, printing “Value is 4, 5, or 6”.
  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 the value does not match any of the previous cases, this block of code is executed, printing “Value is something else”.
  7. The code then calls the process_data function 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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading