Built-in Functions and Types
next()
Advance an iterator and obtain one item. Exhaustion raises StopIteration unless a default was explicitly supplied; then the default is returned. This function requires an iterator, not just an iterable. Other exceptions raised by the iterator are not suppressed by a default. The iterator state changes after a successful advance.
Formal Syntax & Structure
python syntaxpython
next(iterator, /)next(iterator, default, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| iterator | iterator | Object implementing __next__(). |
| default | object | Fallback for exhaustion only; optional. |
Return Value
object
Next yielded value or supplied default; may be None.
Working Example
Example Snippetpython
items = iter(["draft", "review"])print(next(items))print(next(items))print(next(items, "finished"))