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 the next() function in Python is:

next(iterable, default)

The "default" parameter is used to give or return the value given to this parameter in the event that the specified iterable has reached its end.

Python next() function example

Here is an example of the 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

Advantages of the next() function in Python

Disadvantages of the next() function in Python

Python Online Test


« Previous Function Next Function »