Coolcrab
Coolcrab

Reputation: 2723

python if statement not equal to certain numbers

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)

Upvotes: 0

Views: 265

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180471

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

Upvotes: 3

Ashish
Ashish

Reputation: 266

how about

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

Upvotes: 0

Related Questions