Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Iterator Lazy Evaluation: How It Works

Understand how Python iterators use lazy evaluation to compute values on demand, reduce memory usage, and process large or infinite sequences.

~6 min readPython
#Python iterators#lazy evaluation#generator functions#itertools
Diagram showing a Python iterator producing values one at a time from a data source, illustrating lazy evaluation.

Python iterators are lazy by design: they produce one value at a time, only when asked. An iterable is an object that can produce an iterator when passed to iter(); an iterator is the object that produces the stream of values. This laziness is what allows Python to handle sequences that would not fit in memory, or that are infinite in length. Understanding the distinction between iterables and iterators, and the role of generators in implementing lazy evaluation, is central to writing memory-efficient Python code.

What Lazy Evaluation Means for Python Iterators

Lazy evaluation means that a value is computed only at the moment it is needed, rather than eagerly building a full collection in memory. In Python, an iterator is an object that implements __next__(), returning the next element or raising StopIteration when exhausted. Per the iterator protocol, it should also implement __iter__(), returning itself so a for loop can consume it. The iterator protocol does not require the elements to exist ahead of time; they can be generated on the fly.

Consider the difference between a list and an iterator:

python
numbers = [x * x for x in range(10)] # eager: computes all 10 squares immediately squares = (x * x for x in range(10)) # lazy: computes each square when requested

The list comprehension creates a list containing all ten squared values. The generator expression creates a generator object that computes one square at a time as you iterate over it. The generator is an iterator, and its evaluation is deferred until next() is called.

How Generators Implement Lazy Evaluation

Generator functions are the most common way to create lazy iterators in Python. A function that contains a yield statement becomes a generator function. When called, it returns a generator object without executing any of the function body. Each call to next() runs the function until the next yield, then suspends execution, preserving local state.

python
def read_lines(file_path): with open(file_path) as f: for line in f: yield line.strip()

Calling read_lines() does not open the file; the file is opened on the first next() call. Lines are then read and stripped one at a time. If the file is huge, this avoids loading the entire content into memory.

Generator expressions provide a concise syntax for simple lazy sequences. They behave like generator functions but are limited to a single expression. For more complex logic, a generator function is usually clearer.

Memory Behavior and Runtime Cost

The primary benefit of lazy evaluation is memory efficiency. An iterator that generates values on demand does not need to hold the entire sequence in memory at once. This is critical when working with large datasets, such as log files, database cursors, or network streams.

The tradeoff is runtime overhead. Each next() call involves resuming the generator's frame, which is often slower than indexing into an existing list. For small, finite sequences, the overhead is negligible. For very large sequences, the memory savings usually outweigh the extra CPU cost.

There is also a difference in how the two approaches interact with other operations. A list supports random access, slicing, and repeated iteration. An iterator is single-pass: once consumed, it is exhausted. If you need to iterate over the same data multiple times, you must either recreate the iterator or materialize it into a list, which defeats the memory benefit.

Practical Patterns: itertools and Infinite Sequences

The itertools module provides building blocks for lazy composition. Functions like itertools.count, itertools.cycle, and itertools.islice work with infinite or large sequences without precomputing values.

python
from itertools import islice, count # Infinite counter starting at 10, step 2 for value in islice(count(10, 2), 5): print(value)

This prints 10, 12, 14, 16, 18. count() never stops on its own, but islice limits how many values are consumed. This pattern is useful for generating IDs, paginating data, or implementing retry logic with backoff.

Another common use is chaining lazy iterators to build processing pipelines:

python
from itertools import islice numbers = (x for x in range(1000)) even_squares = (x * x for x in numbers if x % 2 == 0) first_ten = islice(even_squares, 10)

Each stage of the pipeline is lazy. No intermediate list is created, and the final result is produced incrementally.

Common Pitfalls and Misconceptions

One frequent mistake is assuming that a generator can be reused. A generator is an iterator, and iterators are single-use. Once StopIteration is raised, the generator is exhausted and cannot be restarted. If you need to iterate twice, you must recreate the generator by calling the generator function again.

Another misconception concerns variable binding. In a generator expression, the outermost iterable is evaluated immediately, but the per-item expression is evaluated on each next() call. Variables from an enclosing scope used in that per-item expression are looked up at iteration time. A generator expression binds its own loop variable in the generator's local scope. Compare that with lambdas created inside a list comprehension:

python
funcs = [lambda: x for x in range(3)] # all three lambdas see x == 2 gen = (x for x in range(3)) # x is local to the generator

In the lambda case, x is a free variable that refers to the comprehension's loop variable, which ends up as 2. In the generator expression, x is bound to the generator's own local scope, so iterating over gen yields 0, 1, and 2 normally.

Side effects inside a generator are also delayed. If the generator is never fully consumed, the side effects may never happen. This can be useful for resource cleanup, but it can be a bug if you expect the code to run eagerly.

Choosing Between Lazy and Eager Evaluation

The decision to use lazy evaluation depends on the size of the data and the operations you need to perform. Use a lazy iterator when:

  • The sequence is large or infinite, and you process it in a single pass.
  • You want to avoid holding all values in memory simultaneously.
  • You are building a pipeline where each stage filters or transforms values.

Use an eager list when:

  • You need random access to elements by index.
  • You need to iterate over the data multiple times.
  • The sequence is small enough that memory is not a concern.
  • You need to know the length of the sequence before processing.

There is also a hybrid approach: materialize a lazy iterator into a list only when necessary. For example, list(generator) forces evaluation, which is useful when you need to sort or reverse the data. Sorting requires all elements, so a generator alone is insufficient.

When Lazy Evaluation Adds Unnecessary Overhead

Lazy evaluation is not always the best choice. If the sequence is small and you need to access elements multiple times, the overhead of resuming a generator can make the code slower than using a simple list. Additionally, some operations, like len() or indexing, are not available on iterators. You must convert to a list first, which negates the memory benefit.

Another scenario is when the generator function performs expensive setup that is repeated on every call. If you call the generator function multiple times, the setup runs each time. In such cases, it may be more efficient to compute the values once and store them, if memory allows.

Finally, debugging lazy code can be harder. If an exception is raised inside a generator, the traceback includes the generator frame and the next() call site, but the generator's state is suspended between calls, so the full context of what had been computed can be less obvious than with a list. Adding logging inside the generator or stepping through it with a debugger can help.

Understanding lazy evaluation in Python iterators is not just about knowing that generators exist. It is about recognizing when the deferred computation model fits your problem and when it adds complexity without benefit. For large or infinite data, lazy iterators are often the only practical approach. For small, finite collections, an eager list is usually simpler and faster. The choice should be driven by the access patterns and memory constraints of your specific use case.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.