Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python next() Function

Python next() Function

Python built-in function Python built-in 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:

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

)

print

(

x

)

except

StopIteration

:

Exit the loop when StopIteration is encountered

break

The output is:


1
2
3
4
5

Python built-in function Python built-in function