Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9280119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:47:41+00:00 2026-06-18T17:47:41+00:00

Possible Duplicate: Replacements for switch statement in python? Suppose I have a list in

  • 0

Possible Duplicate:
Replacements for switch statement in python?

Suppose I have a list in Python:

list = (‘ADD’, ‘SUB’, ‘PUSH’, ‘POP’)

I want to run a function depending on input, and that input can be any value in the list.

Instead of writing a switch case statement for each element in list, is there a more compact way of writing it?

My reasoning is for the case of the list growing in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:47:42+00:00Added an answer on June 18, 2026 at 5:47 pm

    Well, there is no switch/case statement in Python.

    For a small list, you want to use if/elif:

    def do_stuff(x, *args):
        if x == 'ADD':
            return do_add(*args)
        elif x == 'SUB':
            return do_sub(*args)
        # …
        else:
            raise RuntimeError('Never heard of {}'.format(x))
    

    For a larger list, you want to make sure each case is a function (I already assumed that above, but if you had code like return args[0] + args[1], you’d have to change that into a do_add function), and create a dict mapping names to functions:

    func_map = {'ADD': do_add, 'SUB': do_sub, … }
    
    def do_stuff(x, *args):
        try:
            return func_map[x](*args)
        except KeyError:
            raise RuntimeError('Never heard of {}'.format(x))
    

    This works because in Python, functions are normal objects that you can pass around like any other objects. So, you can store them in a dict, retrieve them from the dict, and still call them.

    By the way, this is all explained in the FAQ, along with a bit of extra fanciness.

    If you have some default function you’d like to call instead of raising an error, it’s obvious how to do that with the if/elif/else chain, but how do you do it with the dict map? You could do it by putting the default function into the except block, but there’s an easier way: just use the dict.get method:

    def do_stuff(x, *args):
        return func_map.get(x, do_default)(*args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Replacements for switch statement in python? I'm making a little console based
Possible Duplicate: Replacements for switch statement in python? Given this method : def getIndex(index):
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: What is the relative performance difference of if/else versus switch statement in
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.