How to Use next() Function in PythonPython Basics


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.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us