Switch Case Replacements for Python

With the syntactical difference between Python 3.10 & beyond

Many programming languages have switch-case statements.

Unlikely, there are no switch-case statements in Python for versions less than 3.10.

switch is useful especially if you want to return a lot of different values based on a specific input.

When I first started programming with C, switch statements made sense. After I switched to Python, I found there was no such thing. I needed to find a workaround or something to do many different things based on one value.

I first used if statements as a replacement for switch cases. It was ugly, unreadable, and not efficient.

Until I knew there are some other ways to do this to make my code more readable and efficient as well.

In this tutorial, I will show you how to replace switch statements for new Python 3.10 and older versions.

Use dictionary as a mapper (for Python < 3.10)

If you are using a Python version less than 3.10, you can use a dictionary as a mapper.

For example, you can use a dictionary to map numbers to their associated words like the following:

digits_mapper = {
    0: "zero",
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine",

print(digits_mapper.get(5, "nothing"))
# five

print(digits_mapper.get(10, "not existing"))
# not existing

In the above example, we used the get method. It returns the value associated with the key. If the key is not in the dictionary, it will return the value specified in the second argument.

Let's say there are too many cases to check. Using a dictionary is more efficient than the usual fall-through behavior for switch.

In other languages, the compiler checks all cases in order until it finds a break statement; or it reaches the default case.

For dictionaries, when we use get method the compiler only checks the key passed. It then returns the value associated with that key.

Let's see a more complex example: If you want to calculate taxes based on many countries. Consider using dictionaries. This time you can define each country's tax rate calculation in a separate function. Pass that function to a dictionary like the following:

def egypt(amount):
    calculate_tax = amount * 0.05 # or insert the tax equation
 return calculate_tax

def palestine(amount):
    calculate_tax = amount * 0.02 # or insert the tax equation
 return calculate_tax

country_tax_calculate = {
 "egypt": egypt,
 "palestine": palestine

def calculate_tax(country_name, amount):
 return country_tax_calculate[country_name](amount)

print(calculate_tax("egypt", 8000))

When you used a country_tax_calculate dictionary, it made your code more elegant. Readable. And also more efficient.

You used that dictionary in calculate_tax() function passing the country_name key to it.

Note that the key is a function (e.g. egypt) and the argument passed to it is the amount value.

Use match-case (for Python 3.10 and above)

In 2006 in Python 3.0 version, Guido von Rossum (the creator of Python) rejected a switch/case statement.

Until in September 2020 (Python 3.10) he and Brandt Bucher (a software engineer at Microsoft) introduced a structural pattern matching.

This introduced using a match/case statement; the first-class implementation for a switch case in Python.

Let's look at the following snippet:

def f(digit):
    match digit:
        case 1:
            return "one"
        case 2:
            return "two"
        case 3:
            return "three"
        case 4:
            return "four"
        case 5:
            return "five"
        case 6:
            return "six"
        case 7:
            return "seven"
        case 8:
            return "eight"
        case 9:
            return "nine"
        case _:
            return "not existing" # the default case when a digit is not found

# five

# not existing

Pattern matching is very efficient. In fact, it is more efficient than the workaround in the previous section.

It is not just a switch case statement. If you're interested, read more on this in the Python documentation.

The case _ statement at the end of the match/case statement is like the default case in other languages. Use it when the value is not found in the match-case statement. Unlike switch cases in other languages, match-case doesn't fall through to the next case if the value is already found.

For example, in the previous example when we check if 5 existed or not. The matcher would return five and would not go through the other cases as the usual switch case. Efficient, isn't it?


We have discussed alternative ways to do switch statements in Python. We've explored that for Python versions older than 3.10 or 3.10 and above. We've seen the dictionary workaround and finally the match-case statement. Both are performant and more pythonic than the chain of if-elif statements.

Published on medium

Buy me a cup of coffee

Join the conversation

Download the ebook

Download the eBook to write cleaner Python code

Get the ebook