Python built-in functions (44) -- next, python built-in 44 next
English document:
-
next
(
Iterator[,
Default])
-
Retrieve the next item from
IteratorBy calling its
__next__()
Method. If
DefaultIs given, it is returned if the iterator is exhausted, otherwise
StopIteration
Is raised.
-
Note:
-
-
1. The function must receive an iteratable object parameter. Each call returns the next element of the iteratable object. If all elements have been returned
StopIteration
Exception.
>>> a = iter('abcd')>>> next(a)'a'>>> next(a)'b'>>> next(a)'c'>>> next(a)'d'>>> next(a)Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> next(a)StopIteration
2. the function can receive an optional default parameter. After the default parameter is input, if there are still elements in the iteratable object that are not returned, its element values are returned in turn. If all elements have been returned, returns the default value specified by default instead of throw.StopIteration
Exception.
>>> a = iter('abcd')>>> next(a,'e')'a'>>> next(a,'e')'b'>>> next(a,'e')'c'>>> next(a,'e')'d'>>> next(a,'e')'e'>>> next(a,'e')'e'