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
- 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,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))Copy to clipboardCopy to clipboard
Here is the output:
Hi27Python10Copy to clipboardCopy 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 clipboardCopy to clipboard
Here is the output:
Hi27Python10That's all folksCopy to clipboardCopy 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 usVisit usCodeHide codeOutputHide outputCopy 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 lineCopy to clipboardCopy 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
