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,639
  • 5
  • 37
  • 62
Coolcrab
  • 2,469
  • 9
  • 35
  • 55

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
  • 168,988
  • 22
  • 228
  • 312
0

how about

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