Iteration

Generators

yield creates an iterator that produces values on demand.

A generator function is a convenient way to write your own iterator. yield produces one value, pauses the function, and resumes when the next value is requested.

Generators are useful for pipelines, large inputs, and infinite sequences because they avoid building an entire collection in memory.

Use next() to request one value manually, or loop over the generator to consume values until it is exhausted.

Calling a generator function returns an iterator. next() asks for one value and resumes the function until the next yield.

Source

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

numbers = countdown(3)
print(next(numbers))
print(next(numbers))

Output

3
2
The cell's two next() calls on countdown(3), one timeline each: every call resumes the body to the next yield gate, and the local n survives the pause.

A for loop repeatedly calls next() for you. The loop stops when the generator is exhausted.

Source

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

Output

3
2
1

return builds the entire result before handing it back; yield produces values on demand. The list keeps its values for repeated use, while the generator is exhausted after one pass.

Source

def countdown_eager(n):
    result = []
    while n > 0:
        result.append(n)
        n -= 1
    return result

values = countdown_eager(3)
print(values)
print(values)

stream = countdown(3)
print(list(stream))
print(list(stream))

Output

[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[]

Every generator is an iterator. The same countdown written by hand is a class that needs __iter__ and __next__ and an explicit StopIteration. The generator function expresses the same protocol with one yield.

Source

class Countdown:
    def __init__(self, n):
        self.n = n

    def __iter__(self):
        return self

    def __next__(self):
        if self.n <= 0:
            raise StopIteration
        value = self.n
        self.n -= 1
        return value

print(list(Countdown(3)))

Output

[3, 2, 1]

Notes

See also

Run the complete example

Example code

Expected output

3
2
3
2
1
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[]
[3, 2, 1]

Execution time appears here after you run the example.