Python next() function

Summary: in this tutorial, you’ll learn how to use the Python next() function and how to use it to retrieve the next item from an iterator.

Introduction to the Python next() function

An iterator is an object that implements the iterator protocol which consists of two methods:

  • __iter__() method that returns the iterator object itself.
  • __next__() method that returns the next item. If all items have been returned, the __next__() method raises a StopIteration exception.

The next() function get the next item from an iterator by calling its __next__() method.

Here’s the syntax of the next() function:

next(iterator[, default])Code language: Python (python)

The next() function has two parameters:

  • iterator – this required argument specifies an iterator from which you want to retrieve the next item.
  • default – this is an optional argument. The next() function returns the default if the iterator is exhausted; otherwise, it’ll raise a StopIteration exception.

Python next() function examples

Let’s take some examples of using the next() function.

1) Using Python next() function with an iterator example

The following example defines an iterator that returns a number of random numbers between min and max and uses the next() function to get the next random number from the iterator:

from random import randint


def random_seq(min, max, size):
    for _ in range(0, size):
        yield randint(min, max)


random_numbers = random_seq(1, 100, 3)

r = next(random_numbers)
print(r)

r = next(random_numbers)
print(r)

r = next(random_numbers)
print(r)Code language: Python (python)

How it works.

First, import the randint() function from the built-in random module:

from random import randintCode language: Python (python)

Second, define a generator that returns a number of random integers between min and max:

def random_seq(min, max, size):
    for _ in range(0, size):
        yield randint(min, max)Code language: Python (python)

Third, call the random_seq function:

random_numbers = random_seq(1, 100, 3)Code language: Python (python)

The random_numbers is a generator which is also an iterator.

Fourth, call the next() function three times to get the next item from the random_numbers iterator:

r = next(random_numbers)
print(r)

r = next(random_numbers)
print(r)

r = next(random_numbers)
print(r)Code language: Python (python)

It should display three random numbers between 1 and 100.

If you call the next() function one more time, you’ll get a StopIteration exception:

r = next(random_numbers) # StopIterationCode language: Python (python)

However, if you use a default value, the next() function will return that value instead of raising the StopIteration exception:

r = next(random_numbers, None)
print(r)  # NoneCode language: Python (python)

2) Using Python next() function to skip heading when reading a csv file

The following example uses the built-in csv module to read the country.csv file with the following information:

import csv

with open('country.csv', encoding='utf8') as f:
    csv_reader = csv.reader(f)

    # skip the header
    next(csv_reader)

    # iterate over the data line
    for line in csv_reader:
        print(line)Code language: Python (python)

How it works.

First, import the csv module:

import csvCode language: Python (python)

Second, open the country.csv file and return the reader object from the csv.reader() function. The reader object is an iterator.

with open('country.csv', encoding='utf8') as f:
    csv_reader = csv.reader(f)
    #... Code language: Python (python)

Third, call the next() function to retrieve the header from the csv_reader iterator:

next(csv_reader)Code language: Python (python)

Finally, display all the data lines:

for line in csv_reader:
    print(line)Code language: Python (python)

Summary

  • Use the Python next() function to get the next item from an iterator.
Did you find this tutorial helpful ?