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 7748773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:52:10+00:00 2026-06-01T10:52:10+00:00

I often end up writing code like if x == 1 or x ==

  • 0

I often end up writing code like

if x == 1 or x == 5 or x == 10 or x == 22 :
    pass

In English it seems redundant to keep repeating x, is there an easier or shorter way to write out an if-statement like that?

Maybe checking of existence of x‘s value in a tuple ( 1, 5, 10, 22, ) or something?

  • 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-01T10:52:11+00:00Added an answer on June 1, 2026 at 10:52 am

    Yes, you are right – either in a tuple or (if this check is made repeatedly) in a set.

    So either do

    if x in (1, 5, 10, 22):
        pass
    

    or, if you do this check often and the number of values is large enough,

    myset = set((1, 5, 10, 22))
    
    [...]
    
    if x in myset:
        pass
    

    The myset stuff is the more useful, the more values you want to check. 4 values are quite few, so you can keep it simple. 400 values and you should use the set…

    Another aspect, as pointed out by Marcin, is the necessary hashing for lookup in the set which may be more expensive than linearly searching a list or tuple for the wanted value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our project, we often end up writing complex joins over like 3 tables.
I often end up with blocks of code like this: public class CustomFile {
Often I find myself interacting with files in some way but after writing the
Often when writing file writers or parsers in c++, I would like to monitor
When I am writing code in Python, I often need to remove items from
I often use the website www.cplusplus.com as a reference when writing C code. I
When I program I often end up making 5 or 6 different versions so
In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and
I do the front end HTML for a JSF team. I often run into
This is a problem I come across every so often; I always end up

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.