For the built-in python containers (list
, tuple
, etc) the in
operator is equivalent to any(y == item for item in container)
with the caveat that the former method is faster (and prettier):
In [13]: container = range(10000)
In [14]: %timeit (-1 in container)
1000 loops, best of 3: 241 us per loop
In [15]: %timeit any(-1 == item for item in container)
1000 loops, best of 3: 1.2 ms per loop
Is there an equivalent to any(y is item for item in container)
? That is, a test that uses is
instead of ==
?
list.index
andlist.count
etc say something to the effect "Return the index in the list of the first item whose value is x. It is an error if there is no such item" – Dexterous