Python next()
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
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,StopIterationis 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))
Here is the output:
Hi27Python10
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"))
Here is the output:
Hi27Python10That's all folks
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:
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:
with open('sample.txt') as f:print(next(f)) # Reads the first line
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
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 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours