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,520 points
776 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,100 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, 2022 by Soham
• 9,710 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,460 points
656 views
0 votes
0 answers

Replacements for switch statement in Python?

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

Apr 22, 2022 in Python by Edureka
• 13,620 points
349 views
0 votes
0 answers

Replacements for switch statement in Python?

I want to create a Python function ...READ MORE

Sep 21, 2022 in Python by Samuel
• 460 points
509 views
0 votes
1 answer

Replacements for switch statement in Python?

To get the same function as a ...READ MORE

answered Feb 16, 2023 in Python by Rishu
• 300 points
1,142 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 4,358 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
3,926 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,100 points
920 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,100 points
723 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP