Python next() FunctionPython Functions

Difficulty Level:
Difficulty Level:

The next() function returns the next item of an iterator.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

mylist = iter(["A", "B", "C"])
a = next(mylist)
print(a)
a = next(mylist)
print(a)
a = next(mylist)
print(a)

Output:

A
B
C

Syntax

next(iterator[, default])

Parameters

Name Description
iterator An iterable object.
default A default value to return if the iterator has reached its end.

Return Value

It returns the next item from an iterator.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us