Skip to main content

Explain Python iterators and generators.

Entry Python
Quick Answer Iterator: object with __iter__() and __next__() methods that produces values one at a time. Generator: function using yield that automatically becomes an iterator - pauses at each yield and resumes from there. Generators are lazy (compute on demand, memory efficient). Use generators for large sequences where computing all values upfront would be expensive. Generator expression: (x**2 for x in range(10)).

Answer

Iterator: implements __iter__() and __next__().
Generator: uses yield for lazy evaluation.
Efficient for large datasets.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice