pythonpython-3.xpattern-matching

Is there a way to include "and" operator in a case/match condition?


According to Python documentation wa can use the OR operator in a match/case as follows:

match True:
    case(condition_1 | condition_2):
        # code

I'm wondering if there is a way to do the same thing with the AND operator, like in this example:

match True:
    case(condition_1 & condition_2):
        # code

Thank you in advance


Solution

  • How about doing something like this?

    cond1 = 'key' in dict_1
    cond2 = 'key' in dict_2
    
    match (cond1, cond2):
        case (True, True):
            ...
        case (True, False):
            ...
        case (False, True):
            ...
        case (False, False):
            ...