Python next() Function
Description
next()Returns the next item of the iterator.
next()The function should be used together with theiter()function that generates the iterator.
Syntax
next() syntax:
next(iterable[, default])
Parameter description:
- iterable -- iterable object
- default -- optional, used to set the default value to return when there is no next element. If not set and there is no next element, a StopIteration exception will be triggered.
Return Value
Returns the next item.
Examples
The following example demonstrates the usage of next():
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# First obtain the Iterator object:
it = iter([1, 2, 3, 4, 5])
# Loop:
while True:
try:
# Get the next value:
x = next(it)
print(x)
except StopIteration:
# Exit the loop when StopIteration is encountered
break
The output result is:
1 2 3 4 5Other Extensions
Python Built-in Functions