Home / Python / Reference / Built-in functions

Python next() function

Take the next item from an iterator.

Syntax

next(iterator, default)

Raises StopIteration when the iterator is used up, unless you give a default. next(x for x in items if …) finds the first matching item.

Parameters

iteratoran iterator
defaultreturned when there are no more items

Return value

the next item

Example

Code

it = iter([10, 20])
print(next(it), next(it), next(it, "done"))
nums = [3, 8, 11, 14]
print(next(n for n in nums if n > 9))
print(next((n for n in nums if n > 99), None))

Output

10 20 done
11
None

Related

Learn it step by step in the free lesson Pythonic Python, with exercises that check your answers.