Replacements for switch statement in Python

0 votes
If i have to write a function in Python that returns different fixed values based on the value of an input index.

In other languages I would use a switch or case statement, but Python does not appear to have a switch statement. What are the recommended Python solutions?
Aug 10, 2018 in Python by bug_seeker
• 15,530 points
180 views

2 answers to this question.

0 votes

If you'd like defaults you could use the dictionary get(key[, default]) method:

def f(x):
    return {
        'a': 1,
        'b': 2
    }.get(x, 9)    # 9 is default if x not found
This would help.
answered Aug 10, 2018 by Priyaj
• 58,080 points
0 votes

The recommended path for Python 3.10 which is for 2021 which was  introduced with the match-case statement which will give access to a first-class implementation of a "switch" for Python. The example below should help you with clarity:

def f(x): 
    match x: 
            case 'a': 
                    return 1 
            case 'b': 
                    return 2 
            case _: 
                  return 0 # 0 is the default case if x is not found

The match-case statement is considerably more powerful than this simple example. You could also try using a dictionary:
 

def f(x): 
        return { 
              'a': 1, 
              'b': 2, 
          }[x]

answered Feb 9 by Soham
• 8,740 points

Related Questions In Python

0 votes
1 answer

Replacements for switch statement in Python?

You could use a dictionary: def f(x): ...READ MORE

answered May 29, 2018 in Python by aryya
• 7,440 points
193 views
0 votes
0 answers

Replacements for switch statement in Python?

I want to write a function in ...READ MORE

Apr 22 in Python by Edureka
• 10,240 points
37 views
0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,540 points

edited Dec 6, 2018 by Nymeria 15,438 views
0 votes
1 answer

Unique identification for data items in Python

Try the UUID module of Python. For example, ...READ MORE

answered Apr 17, 2018 in Python by Nietzsche's daemon
• 4,260 points
554 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,917 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,143 views
0 votes
1 answer

What is the Python equivalent for a case/switch statement?

if x == 'a':  # Do the ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 58,080 points
503 views
0 votes
1 answer

What is the flow control for “continue” in python?

This is the way "continue" statement works! You ...READ MORE

answered Jul 16, 2018 in Python by Priyaj
• 58,080 points
277 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP