Home >>Python Built-in Functions >Python next() Function
Python next() function is used to return the next item in a given iterator. You can also add a default return value to return if the iterable has reached to its end.
Syntax:next(iterable, default)
Parameter | Description |
---|---|
iterable | This is a required parameter. It defines an iterable object. |
default | This is an optional parameter. It defines an default value to return if the iterable has reached to its end. |
mylist = iter(["Abhimanyu", "Rahul", "Aman", "Ravi", "Rohit"])
x = next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)
mylist = iter(["Abhimanyu", "Rahul", "Aman"])
x = next(mylist, "Rohit")
print(x)
x = next(mylist, "Rohit")
print(x)
x = next(mylist, "Rohit")
print(x)
x = next(mylist, "Rohit")
print(x)