Python next: The Complete Guide

Python next() method returns the next element from the list; if not present, prints the default value. If the default value is not present, raise the StopIteration error. You can add a default return value to return if the iterable has reached its end.

Python next function

The next() is a built-in Python function that returns the next item from an iterator. The next() function takes two arguments, an iterator, and a default value, and returns an element and returns the next item from the collection. The next() method calls on the iterator and throws an error if no item is present. To avoid the error, we can set a default value.

Syntax

next(iterator, default)

Parameters

  1. iterator: The next() retrieves the next item from the iterator.
  2. default (optional): The default value is returned if the iterator is exhausted (no items left).

It returns an item from the collection.

Let’s see some examples of the next() function to understand its functionality.

# app.py

data = ['eleven', 'mike', 'dustin', 'caleb', 'noah']

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))

See the following output.

➜  pyt python3 app.py
eleven
mike
dustin
caleb
noah
➜  pyt

We got an error from the last statement in the above program if we tried to get the next element when no next element was available (the iterator is exhausted).

In such cases, you can give the default value as the second parameter.

Iterator With Default Value

See the following example in which we define a Python list with only two elements, then use the next() function to add three more items and print in the output.

# app.py

data = ['eleven', 'mike']

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator, 'caleb'))
print(next(dataIterator, 'dustin'))
print(next(dataIterator, 'noah'))

See the following output.

➜  pyt python3 app.py
eleven
mike
caleb
dustin
noah
➜  pyt

Python Tuple with next() Method

Let’s define Python Tuple and apply the next() function on that tuple iterator and see the output.

# app.py

data = ('eleven', 'mike')

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))

See the output.

➜  pyt python3 app.py
eleven
mike
➜  pyt

Python next() Function with the iter() function

We have used the Python iter() function used to convert an iterable to an iterator.

See the following code example.

# app.py

data = iter(['millie', 'finn'])

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))

See the output.

➜  pyt python3 app.py
millie
finn
➜  pyt

Note that For loop is the better choice when printing the contents of the list than next().

The next() is the utility function for printing the components of the container of iter type.

Its usage is when the size of the container is not known, or we need to give a prompt when the list/iterator has exhausted.

The method next() is used when the file is used as the iterator, typically in the loop, the next() method is called repeatedly. This method returns the next input line or raises StopIteration when the EOF is hit.

Combining the next() method with other file methods like readline() does not work right. However, the seek() to reposition the file to an absolute position will flush the read-ahead buffer.

That’s it for the next() function in Python.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.