ScanSkill

next

This next() function retrieves the next item from the iterator by calling its next() method.

Syntax

next(iterator[, default])

Here,

  • iterator: Required. Retrieves next item from iterator
  • default: Optional. This is the value that is returned after retrieving the last item instead of the StopIteration error.

Examples

  • Without default parameter
>>> i = iter([1, 2])
>>> next(i)
1
>>> next(i)
2
>>> next(i)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
StopIteration
  • With default parameter
>>> i = iter([1, 2])
>>> next(i, 'No more items')
1
>>> next(i, 'No more items')
2
>>> next(i, 'No more items')
'No more items'