Generators and iterators
How a for loop gets its items one at a time, and how yield lets a function pause to hand out values. Seven steps, from the iterator protocol to an endless sequence.
In 30 seconds
An iterator hands out the items of a sequence one at a time: each call to next returns the following item, until it signals that none are left.
A generator is an iterator written as a function. At each yield it hands over a value and pauses, keeping its variables, then carries on from that spot when asked again.
Because values are produced only on request, a generator can describe a huge or even endless sequence while holding just one value in memory.
Generators and iterators, step by step
Python shown; JavaScript has the same idea with function* and yield. Memory figures were measured on 64-bit CPython 3.13 and will vary. A generator can be walked through only once; the bars are not to scale.
- Step 1: A loop asks for one at a time. A for loop does not take a whole list at once. It first asks the list for an iterator: a small object that remembers a position. Then it calls next on that iterator, again and again, and gets one item each time. Here the first call returns 10 and the position moves on.
- Step 2: Until the iterator says stop. Two more calls return 20 and 30. The fourth call has nothing left to give, so it raises StopIteration, which a for loop quietly treats as the end. This small contract is the iterator protocol: iter gives you an iterator, and next gives the following item or signals that there are no more.
- Step 3: A generator function. A function that contains yield is a generator function. Calling it does not run its body. It returns a generator: an iterator whose items will come from running that body, a piece at a time. So far count_up(3) has computed nothing at all.
- Step 4: yield hands out one value. The first next(gen) starts the body. It runs until it reaches yield i, hands the 1 to the caller, and freezes right there. Unlike return, yield does not throw the function away: its local variables and its place in the code are kept inside gen, waiting for the next call.
- Step 5: Resume, then pause again. The second next(gen) wakes the function on the line after yield. It adds 1 to i, loops round, and yields 2. The sequence is lazy: each value is computed only when somebody asks, so 3 does not exist yet. After 3 the function ends and next raises StopIteration, as with any iterator.
- Step 6: A sequence with no end. Because values are made on demand, a generator can describe a sequence that never ends. naturals loops forever, yet nothing hangs: each call runs only as far as the next yield. Three calls give 1, 2 and 3, and the endless rest stays uncomputed. A list of every whole number could not exist.
- Step 7: One value in memory at a time. Round brackets instead of square ones make a generator expression, a one-line generator. The list version builds a million squares before sum sees the first, taking about 40 MB in this measurement. The generator hands sum one square at a time and stays under a kilobyte, however long the sequence.
Try it yourself
Learn this first
Spotted a mistake on this page?