Python TutorialLesson 25Free preview
Python Iterators
This lesson is part of Python · All levels
Summary
An iterator is an object that returns elements one by one, so that large data does not need to be loaded into memory at once. We get the iterator by calling iter() and then taking each value using next() until we stop with the exception of StopIteration. In Python, almost all collections and even output generators are iterable that can be run in a for loop.
Key points
- The iterable is the repeatable object, whereas the iterator provides the element.
- Iter() makes the iterator and next() takes one element next.
- The iterator stops by triggering the StopIteration when the element runs out.
- The Iterator saves memory by processing data one by one.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.