Pattern Matching in Python 3.10. The Switch statement on steroids | by Alan Jones | Towards Data Science

Python 3.10 has implemented the switch statement — sort of. The switch statement in other languages such as C or Java does a simple value match on a variable and executes code depending on that value.
It can be used as a simple switch statement but is capable of much more.
That might be good enough for C but this is Python, and Python 3.10 implements a much more powerful and flexible construct called Structural Pattern Matching. It can be used as a simple switch statement but is capable of much more.
Let’s take the simple switch case. Below is a code snippet that does a switch on a single value. We test it by running it in a loop with the values 1,2,3 and 4.
for thing in [1,2,3,4]:    match thing:        case 1:            print("thing is 1")        case 2:            print("thing is 2")        case 3:            print("thing is 3")        case _:            print("thing is not 1, 2 or 3")
The first thing to notice is the neatness of the syntax. It starts with the keyword matchfollowed by a variable name. Then there is a list of cases beginning with case and followed by the value that is matched. Note the use of colons and indentation.
This is not disimilar to switch/case statement in other languages but unlike C, for example, after the code for a particular case is executed, control jumps to the end of the match statement.
If no match is made then the code in the default case, designated by -, is executed.
And this is the result.
thing is 1thing is 2thing is 3thing is not 1, 2 or 3
No surprises there.
The switch statement is a simple case of pattern matching but Python takes it a bit further. Take a look at this code:
for thing in [[1,2],[9,10],[1,2,3],[1],[0,0,0,0,0]]:    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 _:            print("too many values")
It’s a matchstatement in a loop again but this time this list of values that the loop will iterate through are lists themselves — the first is [1,2]then [9,10] and so on.
The case statements attempt match those lists. The first case match a list with a single element, the second matches a list with two elements, the third one, a list with three elements. The last case is the default.
But it does more than this. It also binds the values that are matched to the identifiers in the case statement. So, for example, the first list is [1,2] and this matches the second case [x,y]. So, in the code that is executed the identifiers x and y take the values 1 and 2, respectively. Pretty good, eh!
So, the result is this:
two values: 1 and 2two values: 9 and 10three values: 1, 2 and 3single value: 1too many values
We know from the first simple program that we can match values and from the one above we can match more general patterns. So can we match patterns that include values? Of course!
for thing in [[1,2],[9,10],[3,4],[1,2,3],[1],[0,0,0,0,0]]:    match thing:        case [x]:            print(f"single value: {x}")        case [1,y]:            print(f"two values: 1 and {y}")        case [x,10]:            print(f"two values: {x} and 10")        case [x,y]:            print(f"two values: {x} and {y}")        case [x,y,z]:            print(f"three values: {x}, {y} and {z}")               case _:            print("too many values")
You can see, for example, in the second case we match a list that has two elements, the first of which has the value 1.
Here’s the result:
two values: 1 and 2two values: 9 and 10two values: 3 and 4three values: 1, 2 and 3single value: 1too many values
But there’s more.
Here is a program that will match a list with any number of elements.
for thing in [[1,2,3,4],['a','b','c'],"this won't be matched"]:    match thing:        case [*y]:            for i in y:                print(i)        case _:            print("unknown")
In the first case the indentifier is starred which means that an entire list can be bound to it and, as we see, it can be iterated through in a loop.
1234abcunknown
There is more but hopefully this article has given you a taste of Structural Pattern Matching in Python 3.10.
Update: I have written about more advanced pattern matching here.
There is a complete description of Structural Pattern Matching on the Python.org website, including a tutorial, in PEP 636. The tutorial is pretty good and I encourage you to read it.
There are quite a few new developments in Python 3.10, apart from Structural Pattern Matching. Several articles explain them, see for example, James Briggs New Features in Python 3.10.
Be warned, however, 3.10 is still only a beta release and while the examples I’ve shown above work perfectly well, you should not use Python 3.10 in anger until its formal release.