next
Get the next item of an iterator.
Python 3.13
next(iterator[, default])- [ ]optional part
- iterator
- The iterator to take the item from.Iterator
- default
- What to return when the items run out, instead of raising StopIteration.AnyOptional
Return value
The next item; raises StopIteration when the items run out and default is not given.
Examples
First item of an iterator
Python 3.13
it = iter([1])
print(next(it))1
Default value after the last item
Python 3.13
it = iter(['a'])
print(next(it))
print(next(it, None))a None
Details
A list or a string must be turned into an iterator with iter() first: next([1, 2]) raises TypeError.
