Python Next Function (With Examples)

Hey there! For today’s tutorial, we will learn what the Python Next() function is and how it works. We will learn it with the help of examples.

What is Python next() Function?

Python next() function is used to fetch the next item from the collection. It takes two arguments an iterator and a default value and returns an element.

Here’s the example program of python next() function:

marks = [91, 92, 93, 94, 95]

# convert list to iterator
iterator_marks = iter(marks)

# the next element is the first element
marks_1 = next(iterator_marks)
print(marks_1)

# find the next element which is the second element
marks_2 = next(iterator_marks)
print(marks_2)

When you run the program, this will be the output:

91
92

Also read: Deque Python

Python next() Syntax

The syntax of next() function in python is:

next(iterator, default)

next() Parameters

Python next() ParameterDescription
iteratornext() function retrieves the next item from the iterator.
default (optional)This value is returned if the iterator is exhausted ( there is no next item).
next() function parameter in python

next() Return Value

  • The next() function in python returns the next item from the iterator.
  • If the iterator is exhausted, it returns the default value passed an argument.
  • If the default parameter is omitted and the iterator is exhausted. It raises the StopIteration exception.

Get The next() Item From The Iterator

Here’s another example program on how to get the next() item from the iterator.

list1 = [1, 2, 3, 4, 5]

# converting list to iterator
list_iter = iter(list1)

print("First item in List:", next(list_iter))
print("Second item in List:", next(list_iter))

When you execute the program, this will be the output.

First item in List: 1
Second item in List: 2

Passing Default Value To next()

Here’s another example program of Passing Default Value To next()function.

mylist = iter(["Java", "C++", "PHP"])
x = next(mylist, "Python")
print(x)
x = next(mylist, "Python")
print(x)
x = next(mylist, "Python")
print(x)
x = next(mylist, "Python")
print(x)

When you execute the program, this will be the output:

Java
C++
PHP
Python

Python next() StopIteration

Here’s another example program of python next() stopiteration.

list_iter = iter([1, 2])

print("Next Item:", next(list_iter))
print("Next Item:", next(list_iter))

# this line should raise StopIteration exception
print("Next Item:", next(list_iter))

When you execute the program this will be the output.

Traceback (most recent call last):
StopIteration
Next Item: 1
Next Item: 2

While calling out the range of iterator then it returns the stopiterations error. To avoid this error happens, we will use the default value as an argument.

Conclusion

We completely discussed the different functions of next() in python, which we learned in this tutorial with the help of examples in a different function. I hope this simple Python Tutorial can help you to comply with your requirements regarding Python next() functions.

Inquiries

By the way, If you have any questions or suggestions about this article named Python next() Func. Please feel free to comment below. Thank You!

Leave a Comment