Python Reference Library →

Python next() Function – Retrieve Next Item from an Iterator

Let’s look at next() function – retrieve next item from an iterator

  1. Notice

    next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))
Live state

Top-level scope

iterator→Object 1
Object 1 · range_iterator<range_iterator object>
This step

next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

Picture next() function – retrieve next item from an iterator

next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

Image brief

Alt text
A diagram for Python next() Function – Retrieve Next Item from an Iterator. Instructions: iterator = iter(range(3)) print(next(iterator, "done")) print(next(iterator, "done")). Displayed result: 0 1. The final example bindings are iterator --> object 1 (range_iterator). object 1: <range_iterator object>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python next() Function – Retrieve Next Item from an Iterator". Teaching focus: next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS iterator = iter(range(3)) print(next(iterator, "done")) print(next(iterator, "done")) | v DISPLAYED RESULT 0 1 FINAL BINDINGS iterator --> object 1 (range_iterator) object 1: <range_iterator object> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python next() Function – Retrieve Next Item from an Iterator. Instructions: iterator = iter(range(3)) print(next(iterator, "done")) print(next(iterator, "done")). Displayed result: 0 1. The final example bindings are iterator --> object 1 (range_iterator). object 1: <range_iterator object>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))
Live state

Top-level scope

iterator→Object 1
Object 1 · range_iterator<range_iterator object>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0
1

What do you expect this program to print?

What does this example display?

iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0 1

Follow the printed values in order. The output is: 0 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

More examples and reference

Python next() Function

The next() function in Python is used to retrieve the next item from an iterator. It is a key part of working with iterators and loops in Python.

Syntax

next(iterator[, default])

Parameters:

  • iterator – A valid Python iterator object.
  • default (optional) – A value to return if the iterator is exhausted (no more items).

Returns:

  • The next item from the iterator.
  • If default is provided and the iterator is exhausted, it returns the default value instead of raising an error.

Example: Using next() with an Iterator

numbers = iter([10, 20, 30])
print(next(numbers))
10

Calling next() Multiple Times

print(next(numbers))  # 20
print(next(numbers))  # 30
20
30

What Happens After Items Are Exhausted?

print(next(numbers))  # No more items!
Traceback (most recent call last):
  ...
StopIteration

Using default Parameter to Avoid Error

letters = iter(['a', 'b'])
print(next(letters))        # 'a'
print(next(letters))        # 'b'
print(next(letters, 'END')) # returns 'END' instead of raising StopIteration
a
b
END

Use Case: Manual Iteration

The next() function is useful when you need to manually loop over items, especially in custom loops or generators.

my_list = [1, 2, 3]
iterator = iter(my_list)

while True:
    item = next(iterator, None)
    if item is None:
        break
    print(item)
1
2
3

Common Mistakes

  • Using next() on a list instead of an iterator: next([1, 2, 3]) will raise an error.
  • Forgetting to use the iter() function first to get an iterator.

Interview Tip

In coding interviews, next() is often used with generators, custom iterators, or while-loop based parsing.

Summary

  • next() fetches the next item from an iterator.
  • Use the optional default value to avoid StopIteration.
  • Always convert sequences to an iterator using iter() before using next().

Practice Problem

Write a program that manually prints the first three items of a list using next() without using a for loop.

colors = ["red", "green", "blue"]
it = iter(colors)
print(next(it))
print(next(it))
print(next(it))

What to remember

Connect what you noticed in the example with the idea below.

Remember

next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.

Watch out for this idea

next() can directly advance any list without making an iterator.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →