Definition
The next() function returns the next item in an iterator. You can add a default return value, to return if the iterable has reached to its end.
Syntax
next(iterable, default)
Parameters
Parameter | Description |
---|---|
iterable | Required. An iterable object. |
default | Optional. An default value to return if the iterable has reached to its end. |
Examples
myFruits = iter(["apple", "guava", "orange"])
fruit = next(myFruits, "no more fruit")
print(fruit)
fruit = next(myFruits, "no more fruit")
print(fruit)
fruit = next(myFruits, "no more fruit")
print(fruit)
fruit = next(myFruits, "no more fruit")
print(fruit)