0

Is there a way to shorten this if statement?

if i != 9 or i != 23 or i != 25 or i != 33 or i !=35:
    print(i)
2
  • It won't matter much for a few elements, you can use a tuple, list etc.. but if you had a lot of values to check a set will be a lot faster Commented Apr 13, 2016 at 9:32
  • 1
    Thanks, post the answer and I'll accept it. :) Commented Apr 13, 2016 at 9:37

2 Answers 2

3

You can use a set and check if i is not in the set:

 invalid_set = {9, 23,25, 33, 35} 
 if  i not in  invalid_set:
     # all good

A set lookup if O(1) vs O(n) with a list, tuple etc..

Sign up to request clarification or add additional context in comments.

Comments

0

how about

if i not in [9,23,25,33,25]:
    print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.