VOOZH about

URL: https://www.codecademy.com/resources/docs/python/built-in-functions/next

⇱ Python | Built-in Functions | next() | Codecademy


Skip to Content

Python next()

Preview: @Sriparno08 244 total contributions
@Sriparno08

244 total contributions

Published Nov 12, 2022Updated Jul 8, 2025

In Python, the next() function returns the next element from an iterator object. It is especially useful when:

  • Manually iterating through items in a loop
  • Working with generators
  • Handling potentially infinite sequences or data streams
  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 CoursesIncludes 27 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      95 hours95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With CertificateWith Certificate
    • Beginner Friendly.
      24 hours24 hours

Syntax

next(iterator, default)

Parameters:

  • iterator: An object that implements the iterator protocol.
  • default (Optional): A value returned if the iterator is exhausted. If not provided, StopIteration is raised when there are no more items.

Return value:

Returns the next item from the iterator. If the iterator is exhausted and a default is provided, returns the default; otherwise, raises StopIteration.

Example 1: Using next() with an Iterator

In this example, a list called list_items is converted into an iterator object via the iter() function, and each element is printed by means of the next() function:

list_items =iter(["Hi",27,"Python",10])
print(next(list_items))
print(next(list_items))
print(next(list_items))
print(next(list_items))
Copy to clipboard
Copy to clipboard

Here is the output:

Hi
27
Python
10
Copy to clipboard
Copy to clipboard

Example 2: Handling StopIteration with Default Value

This example iterates over a list with the next() function, but prevents the program from crashing with a default parameter:

list_items =iter(["Hi",27,"Python",10])
print(next(list_items,"That's all folks"))
print(next(list_items,"That's all folks"))
print(next(list_items,"That's all folks"))
print(next(list_items,"That's all folks"))
print(next(list_items,"That's all folks"))
Copy to clipboard
Copy to clipboard

Here is the output:

Hi
27
Python
10
That's all folks
Copy to clipboard
Copy to clipboard

Codebyte Example: Using next() with a Generator

This codebyte example uses a generator function that yields numbers in descending order. Here, next() is used to manually fetch each value, and the optional default parameter helps avoid exceptions:

Visit us
Visit us
Hide code
Code
Output
Hide output
Copy to your clipboard

Frequently Asked Questions

1. What happens if next() is called on a non-iterator object?

If you call next() on a non-iterator object, you’ll get a TypeError. The object must be an iterator, or you can convert an iterable into one using iter().

2. What is the difference between for loops and next()?

for loops internally use next() but also handle StopIteration automatically. Using next() gives you manual control over iteration.

3. How is next() useful with files?

File objects are iterators. You can use next() to read one line at a time from a file:

withopen('sample.txt')as f:
print(next(f))# Reads the first line
Copy to clipboard
Copy to clipboard

All contributors

Learn Python on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 CoursesIncludes 27 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      95 hours95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With CertificateWith Certificate
    • Beginner Friendly.
      24 hours24 hours