All of the excellent answers provided here concentrate on the specific requirement of the original poster and concentrate on the if 1 in {x,y,z}
solution put forward by Martijn Pieters.
What they ignore is the broader implication of the question:
How do I test one variable against multiple values?
The solution provided will not work for partial hits if using strings for example:
Test if the string "Wild" is in multiple values
>>> x = "Wild things"
>>> y = "throttle it back"
>>> z = "in the beginning"
>>> if "Wild" in {x, y, z}: print (True)
...
or
>>> x = "Wild things"
>>> y = "throttle it back"
>>> z = "in the beginning"
>>> if "Wild" in [x, y, z]: print (True)
...
for this scenario it's easiest to convert to a string
>>> [x, y, z]
['Wild things', 'throttle it back', 'in the beginning']
>>> {x, y, z}
{'in the beginning', 'throttle it back', 'Wild things'}
>>>
>>> if "Wild" in str([x, y, z]): print (True)
...
True
>>> if "Wild" in str({x, y, z}): print (True)
...
True
It should be noted however, as mentioned by @codeforester
, that word boundries are lost with this method, as in:
>>> x=['Wild things', 'throttle it back', 'in the beginning']
>>> if "rot" in str(x): print(True)
...
True
the 3 letters rot
do exist in combination in the list but not as an individual word. Testing for " rot " would fail but if one of the list items were "rot in hell", that would fail as well.
The upshot being, be careful with your search criteria if using this method and be aware that it does have this limitation.
1
in (tuple) – user9011445any
/all
functions. For example:all([1, 2, 3, 4, False])
will return Falseall([True, 1, 2, 3])
will return Trueany([False, 0, 0, False])
will return Falseany([False, 0, True, False])
will return True – edddif x == 0 or 1:
, which is of course similar toif x or y == 0:
, but might be a little confusing for newbies nonetheless. Given the sheer volume of "Why isn't myx == 0 or 1
working?" questions, I would much rather use this question as our canonical duplicate target for these questions. – Aran-Fey0
,0.0
orFalse
. You can easily write wrong code which gives the "right" answer. – smci