🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Generators (yield)

AI & DATA SCIENCE // generators-yield

A generator function uses yield instead of return to produce a sequence of values lazily, one at a time, pausing its state between each one.

Syntax

def gen():
    yield 1
    yield 2
    yield 3

for value in gen():
    ...

Deep Dive Course

Any function containing a yield statement becomes a generator function: calling it doesn't run the body immediately, it returns a generator object, an iterator that only executes code up to the next yield each time you call next() on it, or iterate over it in a for loop. Between yields, the function's entire local state — variables, the current line, loop position — is paused and preserved, then resumed exactly where it left off. This makes generators ideal for producing large or infinite sequences without holding every value in memory at once.

1Understanding Generators (yield)

Any function containing a yield statement becomes a generator function: calling it doesn't run the body immediately, it returns a generator object, an iterator that only executes code up to the next yield each time you call next() on it, or iterate over it in a for loop. Between yields, the function's entire local state — variables, the current line, loop position — is paused and preserved, then resumed exactly where it left off. This makes generators ideal for producing large or infinite sequences without holding every value in memory at once.

💡

Use a generator instead of building and returning a full list whenever you're processing a large or unbounded sequence, especially if the caller only needs to iterate over it once — it can dramatically reduce memory usage.

editor.html
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for number in countdown(3):
    print(number)
localhost:3000

2Practical Example

Here is a real-world application of Generators (yield) showing how it is used in production Python code.

editor.html
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

fib = fibonacci()
print([next(fib) for _ in range(6)])
localhost:3000

3Best Practices

Follow these guidelines when working with Generators (yield):

1. Use a generator function (or a generator expression) instead of building an entire list in memory when the caller only iterates over the result once

2. Use yield to produce infinite or very large sequences lazily, since a generator computes values on demand rather than all at once upfront

3. Remember a generator is exhausted after one full iteration — convert it to a list explicitly if you need to iterate over the same values more than once

⚠️

Tip: Use a generator instead of building and returning a full list whenever you're processing a large or unbounded sequence, especially if the caller only needs to iterate over it once — it can dramatically reduce memory usage.

editor.html
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for number in countdown(3):
    print(number)
localhost:3000

Examples

Example 01Basic Usage
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for number in countdown(3):
    print(number)
Example 02Advanced Example
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

fib = fibonacci()
print([next(fib) for _ in range(6)])

Best Practices

  • Use a generator function (or a generator expression) instead of building an entire list in memory when the caller only iterates over the result once
  • Use yield to produce infinite or very large sequences lazily, since a generator computes values on demand rather than all at once upfront
  • Remember a generator is exhausted after one full iteration — convert it to a list explicitly if you need to iterate over the same values more than once

Interview Question

Why can a generator represent an infinite sequence, like all Fibonacci numbers, without ever running out of memory?

Hint: Think about when values are actually computed.

A generator doesn't compute or store its values ahead of time — it only runs code up to the next yield when something explicitly asks for the next value, via next() or a for loop, then pauses. Since only the current value and the paused function state need to exist in memory at any moment, a generator can represent a conceptually infinite sequence, and the program only pays for however many values are actually consumed.

Exercises

MediumPractice using Generators (yield) in a real scenario.
View Solution
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for number in countdown(3):
    print(number)

Frequently Asked Questions

Why can a generator represent an infinite sequence, like all Fibonacci numbers, without ever running out of memory?

A generator doesn't compute or store its values ahead of time — it only runs code up to the next yield when something explicitly asks for the next value, via next() or a for loop, then pauses. Since only the current value and the paused function state need to exist in memory at any moment, a generator can represent a conceptually infinite sequence, and the program only pays for however many values are actually consumed.

Related Functions

for-loopdef-keywordlist()