On this page
next
Prev: min | Up: __builtins__ | Next: object
Rel: all, any, enumerate, filter, iter
Ref: docs.python.org — next
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import next
Overview & Signature
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator is exhausted, it is returned instead of raising StopIteration.
Usage
it = iter([10, 20])
assert next(it) == 10
assert next(it) == 20
assert next(it, "done") == "done"
Gotchas
- Pass a
defaultas the second argument whenever exhaustion is expected—cleaner than catchingStopIteration.
REPL Inspect
help(next)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/min Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/object Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/all Linked here; shared tags: #builtins
- tuts/python3/__builtins__/any Linked here; shared tags: #builtins
- tuts/python3/__builtins__/enumerate Linked here; shared tags: #builtins
- tuts/python3/__builtins__/filter Linked here; shared tags: #builtins
- tuts/python3/__builtins__/iter Linked here; shared tags: #builtins
- tuts/python3/tutorials/advanced_python_internals/iterators_and_generators links to this page