Python next() Function

The next() function returns the next item of a specified iterator. For example:

x = iter([12, 23, 34, 45, 56])
print(next(x))
print(next(x))

x = iter((10, 20, 30))
print(next(x))
print(next(x))

The output will be:

12
23
10
20

Note - The iter() function returns an iterator object.

Python next() Function Syntax

The syntax of next() function in Python, is:

next(iterable, default)

The default parameter is used to give/return the value (given to this parameter), in case if the specified iterable has reached to its end.

Python next() Function Example

Here is an example of next() function in Python:

x = iter([12, 23, 34, 45, 56])
print(next(x, 100))
print(next(x, 100))
print(next(x, 100))
print(next(x, 100))
print(next(x, 100))
print(next(x, 100))
print(next(x, 100))

The output will be:

12
23
34
45
56
100
100

Python Online Test


« Previous Function Next Function »



Like/Share Us on Facebook 😋