Function Details: next

Description


Retrieves the next item from the iterator.


Extended Description


The next() function retrieves the next item from the given iterator. If a default value is provided, it is returned when the iterator is exhausted; otherwise, StopIteration is raised. This function is equivalent to calling the __next__() method on the iterator object.


Read More about next from Python Documentation

Function Signature


next(iterator: Iterator, default: Optional[Any] = None) -> Any

Module: builtins

Parameters



Parameter List


  • iterator: Iterator
  • default: Optional[Any]

Return


Returns the next item from the iterator. If the iterator is exhausted, returns the default value if provided, otherwise raises StopIteration.


Return Type


Any

Output

Explanation

This example demonstrates basic usage of next() with a list iterator, including what happens when the iterator is exhausted.