Interview Question

What are iterators?

An iterator produces one item at a time and raises StopIteration when exhausted.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

An iterator is an object that returns successive values through __next__(). • iter() obtains an iterator from an iterable. • next() asks for the next item. • An exhausted iterator raises StopIteration.

💡 Simple Example

iterator = iter([10, 20]) print(next(iterator)) print(next(iterator))

Output

10 20

⚡ Quick Revision

An iterator produces one item at a time and raises StopIteration when exhausted.