next

New In Python 2.6

next(iterator[, default])

Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

Idiomatic

a = -1
for i in range(1, 10):
    if not i % 4:
        a = i
        break

to

next((i for i in range(1, 10) if not i % 4), -1)

References

[1] Docs@Python, 2. Built-in Functions — next(iterator[, default])

results matching ""

    No results matching ""