Repaso
Español
PythonIntermediatePY-43

What is the difference between an iterable, an iterator and a generator?

Answer

  • Iterable: anything you can loop over; implements __iter__ (list, dict, str, file).
  • Iterator: object with __next__ that remembers its position and raises StopIteration at the end; iter(iterable) gives you one. Single pass.
  • Generator: an iterator created by a function with yield (or a generator expression). Its state pauses between yields; values are produced lazily.
  • Why it matters: streaming big files or DB rows with constant memory, pipelines, infinite sequences.

Key terms

Practice this question in Repaso