next() in Python | Python next() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The next() function in python programming is used to get the next item from a collection of values. The next() takes two values as arguments namely - iterator and default-value. The next() function returns the next element of the iterable object as output.

Syntax of next() function in Python

The syntax of next() function is:

The returned value is the next element from the given list.

Note: The next() function calls the __next__() method internally.

Parameters of next() function in Python

As we have discussed, the next() function takes two arguments:

  • iterator: Iterator is an object that is used to iterate over an iterable specified object or collection of values.
  • default: default value is returned from the next() function if the iterator is completed or exhausted. The default value also makes sure that the function should not raise any error. We will learn about the errors raised by the next function later in this article.

Note: The first argument is required, but the second argument is optional.

Return Values of next() function in Python

As we have discussed, the next() function returns the next item present in the specified iterable object. In the case where the iteration is completed (or the iterator gets exhausted), the next() function raises an exception. To avoid the exception, we should use a default value. So, when the iterator is exhausted, the given default value is returned as the next item. The exception raised by the next() function is called StopIteration exception.

Exceptions of next() in Python

The next() method returns the next item in the iterator till there are no items left in the iterator. When the end of an iterator is reached, and we try to access the next item, the python interpreter searches for the default value. If a default value is not given, then the program will raise an error. The exception raised by the next() function is called StopIteration exception.

We can simply say that when we try to get the next item of a collection when no next item is available, a built-in exception StopIteration is raised by the program.

StopIteration is a built-in exception raised by the built-in function next() and an iterator's __next__() method, which resembles that the iterator is exhausted or the iterator produces no further items.

Example of next() function in Python

Let's take an example of a list having numbers as its elements. For using the next() function, we must convert the list into an iterator because the next() function return the next item of the iterator only.

Code:

Output:

Explanation:

The next() function will return 1 to the next_element_1 variable. The iterator will now move to the next index of the list. In the similar manner, 2 will be stored in next_element_2, 3 will be stored in next_element_3, 4 will be stored in next_element_4, and 5 will be stored in next_element_5.

Now, the iterator is exhausted. next_element_default will print -1 because -1 is the specified default value.

Application of next() function in Python

next() is the utility function used for printing the components of an iter-type (iterable) container. We can print the values of a container in numerous ways, like using the range() function. The next() method is used when the size of the container is not known in advance. We can also use the next() function in the case where we need to give a prompt to the user when the iterator has exhausted.

What is the next() function in Python?

The next() function is a very useful method for dealing with the collection of objects or values. The python's next() method takes an iterator and default value in the parameter section. Using the iterable object or iterator specified in the parameter returns the next element of the list. The default value is optional and passed in the parameter so that the next() method does not raise any error. If the iteration is completed or the iterator has exhausted, and we are trying to access the next element from the list, the next() function raises the StopIteration error. So, the specified default value will be returned in the above scenario to avoid exceptions.

More Examples

Let's take some examples to see how the next() method works, the exception raised by the python interpreter when no default value is specified, and the iterator has exhausted.

Example 1: To Get the Next Item from the Iterator

Code:

Output:

Example 2: Passing Default Value to next() function

Code:

Output:

Explanation:

The first three elements of the list are printed by the next() function, and when we tried to get the 4th or next element, it returned the default value.

Example 3: Getting StopIteration exception in next() function

Code:

Output:

Explanation:

The first five elements of the list are printed by the next() function, but when we tried to get the 6th or next element, it raised an exception. An exception was raised because there was no new item in the iterable object, and no default value was provided in the program. So, the error traceback got printed in the output.

Conclusion

  • The next() function is used to get the next item from a collection of values.
  • The next() takes two values as arguments and returns an element as output. The first argument is required, but the second argument is optional.
  • If the iterator has exhausted, and no default value is provided, the function will raise an exception known as the StopIteration exception.
  • next() method is used when the container size is unknown, or we need to give a prompt when the iterator has exhausted.

See Also