You can use in
, or check for the index
and catch the error:
num in a
will check if the item num
is in the list a
.
>>> 1 in [1, 2, 5]
True
>>> 3 in [1, 2, 5]
False
>>> 100 in range(101)
True
try
getting the index, and except
to catch the IndexError
:
def isIn(item, lst):
try:
lst.index(item)
return True
except ValueError:
return False
return False
>>> isIn(5, [1, 2, 5])
True
>>> isIn(5, [1, 2, 3])
False