-1

Generally, there are two variations of writing repeated if conditions that I know of:

  1. Simple
if x != <something> and x != <somethingElse> and x != <somethingElse +>:
  1. Little compact
if all(x != el for el in [<something>, <somethingElse>, <somethingElse +>]

Is there a more compact way?

Rabindra
  • 369
  • 4
  • 14

1 Answers1

1

For x should not be in a group of things, the easiest solution is just:

if x not in (<something>, <somethingElse>, <somethingElse +>):

Expanding it for more items adds little verbosity, and it's relatively efficient, assuming the cost of computing each of the somethings is small (it has to compute them all up front, but then, so does anything aside from one by one comparisons or complicated solutions involving programmatically generatable somethings).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks, nevertheless, your answer will be helpful, In a hurry I typed == instead of !=. So, if you can help with that. Thanks! – Rabindra Sep 18 '20 at 12:46
  • 1
    @Rabindra: Already edited it to answer the fixed question. :-) Though I would have just sent you to the duplicate had I read the fixed question initially. – ShadowRanger Sep 18 '20 at 12:46
  • Sure, thanks. While typing this question, I checked for similar questions. But didn't find any. So, went ahead with posting it. – Rabindra Sep 18 '20 at 12:52