Python next() Function
Description
next() Returns the next item of an iterator.
next() The function is used together with the iterator generated by iter() function.
Syntax
next syntax:
next(iterable[, default])
Parameter Description:
- iterable -- an iterable object
- default -- optional, used to set the value returned when there is no next element. If it is not set and there is no next element, a StopIteration exception is raised.
Return Value
Returns the next item.
Example
The following examples demonstrate how to use 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
)
(
x
)
except
StopIteration
:
Exit the loop when StopIteration is encountered
break
The output is:
1
2
3
4
5
Python built-in function