If you are using Python 2.6+ you should use the next
built-in function, not the next
method (which was replaced with __next__
in 3.x). The next
built-in takes an optional default argument to return if the iterator is exhausted, instead of raising StopIteration
:
next((i for i, v in enumerate(a) if i == 666), None)