Short if statement using or in Python

I have an operator

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

How to write shorter?

+4
source share
3 answers
 if any(x in players for x in ("1111111", "0000000", "blablabla")): # do something 

If you intend to perform many such membership checks, you might consider making players into a set that should not potentially cross the entire sequence on each check.

+4
source

You can use any() :

 things = ["1111111", "0000000", "blablabla"] if any(thing in players for thing in things): 
+4
source
 if any(a in players for a in list_of_tests): #something pass 

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

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

All Articles