Some time ago, I had an 1am brain itch:
If Python's for-loop is slow, and list-comprehension is fast - then by how much?
So I ran a benchmark for this.
Result
I ran the same benchmark for all major python versions that I care about. List comprehension is faster in all of them.
The results are:
- 3.8.20 - faster by 13.51%
- 3.9.25 - faster by 12.19%
- 3.10.12 - faster by 10.96%
- 3.11.14 - faster by 4.13%
- 3.12.12 - faster by 2.85%
Here's a graph of the results:
(blue= for loop, orange = list comprehension. Lower is better.)

Interestingly, python 3.11 has almost closed the performance gap, and 3.12 closed it up even more.
Quoting a comment by kirill-podoprigora, one of the core python devs, on linkedin: "You should run this benchmark on Python 3.12, since comprehensions are now inlined (see PEP 709), which makes them faster."
Benchmark setup
Each benchmark run processed an array of 1 million integers - once using for-loop, then once using list-comprehension.
The benchmark interleaved for loop and list comprehension, spreading the effect of things like CPU throttling, core-switching etc evenly amongst both methods.
The run effectively looked like:
f = for loop
c = list comprehension
fffffffcccccccfffffffcccccccfffffffccccccc ...
[f(x7 times) c(x7 times)] x100 times
1000 benchmark runs were performed sequentially - alternating between for-loop and list-comprehension in each run. This alternation spread the effect of things like thermal CPU throttling, core-switching etc evenly amongst both methods.
This was the code run for the benchmarks:
import time
import csv
import os
import statistics
import platform
DATA = list(range(1_000_000))
INNER_RUNS = 7
COMPARATIVE_BENCHMARK_RUNS = 100
OUTPUT_FILE = "benchmark_results.csv"
python_version = platform.python_version()
def for_loop():
result = []
for x in DATA:
result.append(x * x + 3 * x + 7)
return result
def list_comprehension():
return [x * x + 3 * x + 7 for x in DATA]
def measure_execution_time(fn):
fn() # warm-up
times = []
for _ in range(INNER_RUNS):
start = time.perf_counter()
fn()
times.append(time.perf_counter() - start)
return times
if __name__ == "__main__":
# AIM:
# Run both methods immediately one after another, leading to an interleaved execution
# Looks like: fffffffcccccccfffffffcccccccfffffffccccccc...
# [f(x7 times) c(x7 times)] x100 times
file_exists = os.path.exists(OUTPUT_FILE)
file_empty = (not file_exists) or os.path.getsize(OUTPUT_FILE) == 0
with open(OUTPUT_FILE, "a", newline="") as f:
writer = csv.writer(f)
if file_empty:
print("Output file is empty, so writing CSV header")
writer.writerow(
["python_version", "outer_run", "inner_run", "method", "time"]
)
for outer in range(COMPARATIVE_BENCHMARK_RUNS):
print(f"Running benchmark {outer + 1}/{COMPARATIVE_BENCHMARK_RUNS}")
for_times = measure_execution_time(for_loop)
lc_times = measure_execution_time(list_comprehension)
# write results to csv - for loop
for i, t in enumerate(for_times):
writer.writerow([python_version, outer, i, "for_loop", t])
# write results to csv - list comprehension
for i, t in enumerate(lc_times):
writer.writerow([python_version, outer, i, "list_comprehension", t])
print("Benchmark data written to", OUTPUT_FILE)
System specs
The system used for benchmarking was:
OS: Ubuntu 24.04.4 LTS x86_64
Host: 21ECCTO1WW ThinkPad E14 Gen 4
Kernel: 6.8.0-139-generic
CPU: AMD Ryzen 5 5625U with Radeon G
GPU: AMD ATI 05:00.0 Barcelo
Memory: 38924MiB
Thoughts & opinions
- Brain itch - If Python's for-loop is slow, and list-comprehension is fast - then by how much? 2026 Sep 18 | ~2 pages
- My subconscious doesn't like LLMs 2025 Jul 16 | ~7 pages
- Computers understanding humans makes codebases irrelevant 2023 Apr 08 | ~5 pages
- Own your email's domain 2023 Feb 12 | ~4 pages
- Isolates + storage over http + orchestrators is the future that has arrived 2023 Jan 03 | ~4 pages
Articles
I learn through writing, so I write a lot. Most of these are ever evolving pieces.
API
- HTTP API design handbook 2026 Oct 18 | ~3 pages
Data Engineering
- The Spark Field Manual 2025 Oct 16 | ~23 pages
Python
- Unicode string normalization schemes in Python 2024 May 06 | ~5 pages
Resources
- Bookmarks 2025 Jun 02 | ~4 pages
- PVLDB - links only 2026 Sep 10 | ~162 pages
- PVLDB - links with abstracts (large document) 2026 Sep 10 | ~3377 pages
Work
- Lecture - You and your research by Dr. Richard Hamming 2024 Oct 14 | ~49 pages