Once Python list comprehensions click, you start seeing them everywhere — and your code gets noticeably shorter and clearer for it. They are one of the features that make Python feel like Python. But they are also easy to overuse, so let us cover both how they work and when to stop.
The basic shape
A list comprehension builds a new list from an existing iterable in a single expression. Here is the classic before-and-after:
# The long way
squares = []
for n in range(10):
squares.append(n * n)
# The comprehension
squares = [n * n for n in range(10)]
Read it left to right: “give me n * n, for each n in the range.” Once your eye learns that rhythm, the second version is faster to read, not slower.
Adding a condition
You can filter with an if at the end:
# Only even squares
even_squares = [n * n for n in range(10) if n % 2 == 0]
That reads as “give me n * n for each n, but only when n is even.” Filtering inline like this replaces a loop-plus-if block with one honest line.
Not just lists
The same syntax builds dictionaries and sets, which people forget:
# Dict comprehension
lengths = {word: len(word) for word in words}
# Set comprehension
unique_first_letters = {word[0] for word in words}
And if you wrap the expression in parentheses instead of brackets, you get a generator that produces items lazily — perfect for huge sequences you do not want to hold in memory all at once.
Knowing when to stop
Here is the part tutorials skip. Comprehensions are for building a collection from a transformation and an optional filter. The moment you find yourself nesting three for clauses or cramming a ternary inside a filter, readability falls off a cliff:
# Technically valid, genuinely unpleasant
matrix = [[row[i] for row in grid] for i in range(len(grid[0]))]
If a teammate has to stop and decode it, a plain loop is the better choice. Clever is not the goal; clear is.
The takeaway
Python list comprehensions shine when they replace a small, obvious loop that just maps or filters. Use them there and your code reads beautifully. When the logic grows branches and nesting, write the loop — your future self reviewing this at 5 p.m. on a Friday will thank you.

