PythonMastery
Python programs · Functions · intermediate

Generator function in Python (yield)

A function with yield gives back values one at a time instead of building a whole list. Write a countdown, step through it with next(), and make an endless one.

A generator function uses yield instead of return. Each yield gives back one value and pauses the function. The next time a value is needed, the function continues from where it stopped.

The program

python
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for number in countdown(5):
    print(number, end=" ")
print("Lift off!")
output
5 4 3 2 1 Lift off!

How it works

Step by step with next()

next() asks for one value at a time, which shows the pausing clearly. After the last value, next() raises StopIteration:

python
def countdown(n):
    print("starting")
    while n > 0:
        yield n
        n -= 1
    print("done")

gen = countdown(2)
print(next(gen))
print(next(gen))
try:
    next(gen)
except StopIteration:
    print("no more values")
output
starting
2
1
done
no more values

An endless generator

Because values are produced only when asked for, a generator can go on forever without using more memory. itertools.islice takes the first few values:

python
from itertools import islice

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

print(list(islice(fibonacci(), 10)))
output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

A list of all Fibonacci numbers could never be built. The generator holds only two numbers, a and b, at any time.

Learn the idea: Generators: Lazy Iteration with yield