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

  • Home
  • SEARCH
  • 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 8252535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:35:16+00:00 2026-06-08T00:35:16+00:00

Maybe this question has been asked before, but I couldn’t find it. I am

  • 0

Maybe this question has been asked before, but I couldn’t find it.
I am trying to implement something that determines what range a given value is in. In this example, x may be any real number.

def f(x):
    if x < 0.1:
        do_something_1()
    elif 0.1 <= x < 1:
        do_something_2()
    elif 1 <= x < 10:
        do_something_3()
    elif x >= 10:
        do_something_4()

…you get the idea.

I’ve seen plenty of examples of dictionaries replacing switch statements in Python, but I’ve always understood dictionaries as indexing discrete values.

I find it hard to believe that the if-elif-else chain is the best solution in this situation. Does anyone know of a better one?

  • 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-08T00:35:18+00:00Added an answer on June 8, 2026 at 12:35 am

    Use the bisect package to find the index where the value lies, and then call the appropriate function. In your example:

    import bisect
    
    def f(x):
        funcs = [do_something_1, do_something_2, do_something_3, do_something_4]
        funcs[bisect.bisect_left([.1, 1, 10], x)]()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this has been asked before, but I couldn't find it. My question is
I know that maybe this question has been asked before, but I can't seem
Maybe this question has been asked many times before, but I never found a
I'm sorry if this questions has been asked before, but I couldn't find an
This has been asked before (question no. 308581) , but that particular question and
This question has been asked many times before but none of the previous ones
I know this question has been asked and answered before, but none of the
I'm almost sure this question has been asked before, but for the love of
I feel like this one has been asked before, but I'm unable to find
I know this type of question has been asked before, but I could not

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.