next()
Built-in FunctionPython 2.0+Intermediate
Retrieves the next item from an iterator
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
it = iter([10, 20, 30]) print(next(it)) print(next(it)) print(next(it, "done")) print(next(it, "done"))
next() is a built-in function that retrieves the next item from an iterator.
Try in PlaygroundTags
builtinfunctioncoreiterationgenerator
Related Items
iter()
Built-in Function
Returns an iterator object from an iterable
for
Keyword
Starts a loop that iterates over a sequence or iterable
__next__
Dunder Method
Returns the next value from an iterator; raises StopIteration when done
StopIteration
Built-in Exception
Raised by next() to signal that an iterator is exhausted
yield
Keyword
Pauses a generator function and produces a value to the caller