SyntaxStudy
Sign Up
Python Comprehension Performance
Python Intermediate 4 min read

Comprehension Performance

Comprehension Performance

List comprehensions are faster than equivalent for-loops because they are optimized in CPython. Generator expressions save memory but are slower per element if you need a list anyway.

Example
import timeit

list_comp = timeit.timeit("[x**2 for x in range(1000)]", number=10000)
gen_exp   = timeit.timeit("list(x**2 for x in range(1000))", number=10000)
print(f"List: {list_comp:.2f}s  Gen: {gen_exp:.2f}s")
Pro Tip

Profile before optimizing — readability matters more than micro-optimizations.