next() Next iteration item
Last updated: 2026-09-22
next() Next iteration item
next(iterator[, default]) returns the next item; returns default (or raises StopIteration) when exhausted.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
next(iterator[, default])
⚙️ Parameters
| iterator Required | An iterator. |
|---|---|
| default Optional | Value returned when the iterator is exhausted.(Default:None) |
Returns:The next item, or default when the iterator is exhausted.
💥 Raises
StopIteration— Raised when the iterator is exhausted and no default is given.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
1
2
end
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat happens when next(it) is called on an exhausted iterator?
AIt raises StopIteration; passing the default next(it, 'end') returns 'end' when exhausted instead of raising.
QWhat is the relationship between next() and a for loop?
AA for loop internally calls next() repeatedly until StopIteration is caught; using next() manually suits precise control like 'take only the first' or 'skip a few'.
QWhat is the common use of next(it, None)?
ASafely fetching the first element that meets a condition: next((x for x in lst if cond), None) yields None when there is none, without raising.
QWhat happens when next() is called on an ordinary list?
AIt raises a TypeError — next() only accepts iterators; first convert a list with iter(lst).