lifez
lifez

Reputation: 318

Short if statement with or in Python

I have if statement

if "1111111" in players or "0000000" in players or "blablabla" in players:
    do something

How to write in short ?

Upvotes: 4

Views: 220

Answers (3)

jamylak
jamylak

Reputation: 133504

if any(x in players for x in ("1111111", "0000000", "blablabla")):
    # do something

If you are going to be doing a lot of these membership checks you may consider making players into a set which doesn't need to potentially traverse the entire sequence on each check.

Upvotes: 4

tacaswell
tacaswell

Reputation: 87356

if any(a in players for a in list_of_tests):
   #something
   pass

If you are using numpy.any(), you will need to convert the generator to a list first.

if np.any([a in players for a in list_of_tests]):
   #something
   pass

Upvotes: 2

Blender
Blender

Reputation: 298106

You can use any():

things = ["1111111", "0000000", "blablabla"]

if any(thing in players for thing in things):

Upvotes: 4

Related Questions