Technology Iterators and Generators | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonIterators and Generators
Advanced Python · Chapter 22

Iterators and Generators

Process values lazily instead of building every result at once.

Why this matters

Lazy iteration can reduce memory use and make streaming workflows natural.

Start with the idea

An iterator produces one value at a time. A generator function uses yield to produce a sequence lazily while preserving its execution state between values.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
def countdown(start):
    current = start
    while current > 0:
        yield current
        current -= 1

print(list(countdown(3)))
Expected output
[3, 2, 1]
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Converting a generator to a list immediately when the whole point was to avoid materializing everything.

Better approach

Keep values lazy when downstream code can consume them one at a time.

Quick recap

  • Iterators produce values sequentially.
  • Generators make iterators easy to write.
  • Lazy evaluation can save memory.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Write a generator for even numbers.
  2. Consume it with a for loop instead of list().