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)
RedBaron
  • 4,717
  • 5
  • 41
  • 65
Coolcrab
  • 2,655
  • 9
  • 39
  • 59

2 Answers2

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..

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

how about

if i not in [9,23,25,33,25]:
    print(i)
Ashish
  • 266
  • 2
  • 9