Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))Top-level scope
<range_iterator object>next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.
(no output)
What do you expect this program to print?
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>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))Top-level scope
<range_iterator object>The example has finished. Compare the final output with the values and references above.
0 1
What do you expect this program to print?
iterator = iter(range(3))
print(next(iterator, "done"))
print(next(iterator, "done"))next() FunctionThe 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.
next(iterator[, default])
iterator – A valid Python iterator object.default (optional) – A value to return if the iterator is exhausted (no more items).default is provided and the iterator is exhausted, it returns the default value instead of raising an error.numbers = iter([10, 20, 30])
print(next(numbers))
10
print(next(numbers)) # 20
print(next(numbers)) # 30
20
30
print(next(numbers)) # No more items!
Traceback (most recent call last):
...
StopIteration
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
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
next() on a list instead of an iterator: next([1, 2, 3]) will raise an error.iter() function first to get an iterator.In coding interviews, next() is often used with generators, custom iterators, or while-loop based parsing.
next() fetches the next item from an iterator.default value to avoid StopIteration.iter() before using next().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))
Connect what you noticed in the example with the idea below.
next() advances an iterator by one item. A supplied default handles exhaustion instead of raising StopIteration.
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.