How to use 'in' a python match case statement

60 Views Asked by At

I'd like to check if a value is in a list in the 'case' of a structural pattern matching.

Put it another way, I'd like to translate the following "if else" in a match case statement.

list1 =  [4, 2, 7]
var = 2
if var == 1:
    print("case1")
elif var in list1:
    print("case2")

The following code does not work but shows what I would like to do:

list1 =  [4, 2, 7]
var = 2
match var:
    case 1:
        print("case1")

    case in list1 : # Not working
        print("case2")
0

There are 0 best solutions below