Coding Python

Pipeline — Generators & yield

Six levels on producing values one at a time — and seeing what that saves.

6 levels · Intermediate → Advanced
Level 1

Checks
    Your PythonPython 3
    1

    How to play

    Write the generator and press Run. The values are then pulled through one at a time and shown in the order they were produced — not collected into a list first, because that would defeat the point. Where it is relevant, a memory bar compares your generator against the list holding the same sequence.

    Level 5 is the one to look at: a generator over a million numbers is about 100 bytes, while the list is about 7.6 MB. The generator stays that size whether the sequence is a thousand items or a billion.

    What each level teaches

    1. Your first yield — a function with yield returns a generator.
    2. Never-ending — an infinite sequence, which is only possible because nothing is stored.
    3. No list required — generator expressions, and why sum(x for x in …) beats sum([x for x in …]).
    4. Join the pipes — chaining stages, each holding one value at a time.
    5. Count the cost — the memory comparison, made concrete.
    6. Hand it on — yield from, for delegating to another generator.

    What yield really does

    return ends a function and throws away its local variables. yield hands a value back and pauses, keeping every local exactly as it was. Next time round, the function resumes on the line after the yield. That is why a generator can hold its place in an endless loop without holding the sequence.

    It also means nothing happens when you call the function. countdown(5) runs no code at all; it builds a generator object. The body only starts when something asks for the first value.

    Generator or list?

    Use a list whenUse a generator when
    You need the values more than onceYou pass through exactly once
    You need len() or indexingYou only need to iterate
    The data is small and already in memoryThe data is huge, streamed or endless
    You need to sort or reverse itYou are filtering or transforming on the way past

    The honest caveat: a generator can only be consumed once. Iterate it twice and the second loop sees nothing — there is no error, just silence, which makes it a genuinely confusing bug the first time you hit it. If you need the values again, keep a list.

    Where this shows up in real code

    Reading a large file line by line, paging through an API, processing a log stream, or any chain of filters and transforms over more data than you want to hold at once. range() itself is lazy for exactly this reason — range(10**9) is instant and costs nothing. The written version is in the Python loops tutorial, which covers yield alongside the ordinary ways of iterating.

    Practise generators & yield on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    What does yield do in Python?

    It hands one value back to whoever is iterating and pauses the function, keeping its local variables. The next request resumes from just after the yield.

    What is the difference between a generator and a list?

    A list holds every value in memory at once; a generator produces them one at a time and keeps its place. A generator over a million numbers is about 100 bytes against roughly 7.6 MB for the list.

    What is a generator expression?

    A comprehension in round brackets: (n * n for n in nums). It produces values lazily, so sum(n * n for n in nums) never builds the intermediate list that the square-bracket version would.

    Why is my generator empty the second time I loop over it?

    Because a generator can only be consumed once. After the first pass it is exhausted, and a second loop sees nothing with no error raised. Store the values in a list if you need them again.

    What does yield from do?

    It delegates to another iterable, passing its values straight through to the caller. It replaces a loop that would otherwise re-yield every item one by one, and is how recursive generators are written.

    Keep going

    ← All Python coding games  Read the tutorial →

    Take the games with you

    Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.

    Get it on Google Play
    Coding Python app icon