HomeInterview QuestionsWhat are Python generators and how are they differ…

What are Python generators and how are they different from iterators?

🟡 Medium Conceptual Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

A generator is a special type of iterator that is defined using a function with the yield statement. When called, it returns a generator object that produces values lazily, generating each value only when requested via next() or a loop. Generators maintain their state between yields, allowing efficient memory usage for large or infinite sequences. An iterator, in general, is any object that implements the __iter__() and __next__() methods; it can be a list, a generator, or a custom class. All generators are iterators, but not all iterators are generators. The key differences are: generators use yield to produce values on the fly, whereas iterators may pre‑store all items; generators are defined by syntax, while iterators can be implemented manually; generators automatically handle StopIteration when exhausted. Use generators when you need to process large datasets, stream data, or implement lazy evaluation to save memory.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500