next()
next(it) asks an iterator for its next item; iter(x) gets an iterator from an iterable.
In more detail
Calling them by hand shows what a for loop does behind the scenes. next(it, default) returns the default instead of raising StopIteration at the end.
Example
it = iter([1, 2])
next(it), next(it)Related terms
- iterator — An object that produces the items of a sequence one at a time, on request, instead of all at once.
- iterable — Any object you can loop over with for, such as a list, string, tuple, dictionary, set, range, file or generator.
- StopIteration — The exception an iterator raises from next() to say it has no more items.
Taught in
- What A Program Really Is — Hands-on Python: Complete Video Course & Book
- How Python Runs Your Code — Hands-on Python: Complete Video Course & Book
- What Python Is, And Where It Came From — Hands-on Python: Complete Video Course & Book
- Repeat Loops And Successive Modification — Hands-on Python: Complete Video Course & Book
- Accumulation Loops — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book