A list comprehension is one of Python’s most powerful and Pythonic features — it lets you create a new list by applying an expression to each item in an iterable, often with a filter condition, all in a single, concise line. It’s a readable, expressive alternative to traditional for loops with append(), and in most simple cases it’s faster and clearer. In 2026, list comprehensions remain essential — used constantly for transformations, filtering, flattening, and data preparation in scripts, pandas, Polars, and production pipelines.
Here’s a complete, practical guide to list comprehensions: basic syntax, filtering, nested comprehensions, real-world patterns, and modern best practices with type hints, readability, and when to avoid them.
The basic form is [expression for item in iterable] — it applies the expression to every item and collects the results into a new list.
# Squares of numbers 1 to 10
squares = [x ** 2 for x in range(1, 11)]
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Add a condition with if at the end — only items that pass the test are included.
# Even squares only
even_squares = [x ** 2 for x in range(1, 11) if x % 2 == 0]
print(even_squares) # [4, 16, 36, 64, 100]
Real-world pattern: cleaning and transforming data — very common when processing lists from user input, files, APIs, or databases.
raw_names = [" alice ", "BOB", " charlie ", "david"]
# Clean, capitalize, and filter short names
clean_names = [name.strip().capitalize() for name in raw_names if len(name.strip()) > 3]
print(clean_names) # ['Alice', 'Bob', 'Charlie']
Another everyday use: flattening nested lists or creating pairs — list comprehensions handle nested loops cleanly.
# Flatten a list of lists
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
print(flat) # [1, 2, 3, 4, 5, 6]
# Create coordinate pairs
x_coords = [0, 1, 2]
y_coords = [3, 4]
points = [(x, y) for x in x_coords for y in y_coords]
print(points) # [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4)]
Best practices keep list comprehensions readable, efficient, and maintainable. Keep them short and simple — one expression, one if condition at most; if logic grows (>1–2 lines, needs statements, or multiple conditions), switch to a regular for loop with append(). Use meaningful variable names — [price * quantity for price, quantity in items] — never [x * y for x, y in z]. Add type hints for clarity — list[int] or list[str] — improves readability and IDE/mypy support. Avoid nested comprehensions with multiple for and if — they become hard to read; flatten with loops or itertools.chain() instead. Modern tip: prefer generator expressions (x**2 for x in range(10)) when passing to functions like sum(), max(), or list() — they’re memory-efficient and lazy. In production, avoid side effects inside comprehensions — no print(), file writes, or database calls; keep them pure. Use comprehensions for data cleaning/transforms — they’re often 2–5× faster than equivalent loops.
List comprehensions are Python’s concise, expressive way to build new lists — faster than loops, clearer than map()/filter(), and a hallmark of Pythonic code. In 2026, use them with type hints, keep them simple, and switch to loops for complex logic. Master list comprehensions, and you’ll transform, filter, and prepare data with elegance and speed.
Next time you need a new list from an old one — reach for a list comprehension. It’s Python’s cleanest way to say: “Take this, do that, and give me the results.”