Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

next()

Advances an iterator by one position and returns the item.

Version: Python 3.14; repository image, production runtime not attested

Syntax

next(iterator[, default])

Advances an iterator by one position and returns the item.

An optional default turns exhaustion into a normal return. Without it, exhaustion raises StopIteration.

Requests the next item from __next__. The first call consumes 8. Once the iterator is exhausted the optional default returns the text end, while omitting it raises StopIteration. An actual yielded None does not trigger the default.

it = iter([None])
assert next(it, 9) is None and next(it, 9) == 9
try:
    next(it)
except StopIteration:
    pass
else:
    raise AssertionError

Examples

items = iter([8])
print(next(items))
print(next(items, "end"))

Result

8
end

Requests the next item from `__next__`. The first call consumes 8. Once the iterator is exhausted the optional default returns the text end, while omitting it raises StopIteration. An actual yielded None does not trigger the default.

Sources