← All benchmarksPython
Python generator vs list comprehension memory: under 1 MB vs 204 MB
Do generator expressions use less memory than list comprehensions in Python?
Summing the squares of 5 million integers peaked at 204 MB with a list comprehension and under 1 MB with a generator expression (tracemalloc).
Results
| sum([i*i ...]) | 204 MB peak |
|---|---|
| sum(i*i ...) | < 1 MB peak |
How it was measured
range(5_000_000), tracemalloc peak.
sum(i*i for i in range(5_000_000))Why
A generator produces one value at a time instead of building the full list.
When this does not apply
Use a list if you need len, indexing or multiple passes.
Environment
MacBook Air (Apple silicon, 8 cores), macOS, Python 3.9. Measured 2026-10-12.
Source: shorts/demo-ticket/w3/gen.py in my content-engine repository. Treat any single run as a direction; re-run it on your hardware.
By Josué Barros, senior software engineer (full-stack, data and AI).