'How to do an else (default) in match-case?

Python recently has released match-case in version 3.10. The question is how can we do a default case in Python? I can do if/elif but don't know how to do else. Below is the code:

x = "hello"
match x:
    case "hi":
        print(x)
    case "hey":
        print(x)
    default:
        print("not matched")

I added this default myself. I want to know the method to do this in Python.



Solution 1:[1]

match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>

cf: https://docs.python.org/3.10/whatsnew/3.10.html#syntax-and-operations

Solution 2:[2]

for thing in [[1,2],[2,11],[12,14,13],[10],[10,20,30,40,50]]:
match thing:
    case [x]:
        print(f"single value: {x}")
    case [x,y]:
        print(f"two values: {x} and {y}")
    case [x,y,z]:
        print(f"three values: {x}, {y} and {z}")       
    case _: # change this in default 
        print("too many values")

If you want to read and get more understanding: https://towardsdatascience.com/pattern-matching-in-python-3-10-6124ff2079f0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julien Sorin
Solution 2