Python List Comprehensions Explained: The Ultimate Guide with Examples
Introduction
Python is loved for its clean and concise syntax—and list comprehensions are one of its most powerful features. They allow you to create new lists by transforming or filtering existing iterables with a single line of code.
This guide will walk you through what list comprehensions are, why they’re useful, and how to use them with real-world examples.
What Is a List Comprehension?
A list comprehension is a compact way to generate lists in Python.
Instead of using a loop like this:
squares = []
for x in range(10):
squares.append(x**2)
You can write:
squares = [x**2 for x in range(10)]
It's shorter, easier to read, and often more efficient.
Basic Syntax
[expression for item in iterable]
-
expression – the value to include in the new list
-
item – the current element in the iteration
-
iterable – the collection you loop over
Example:
evens = [x for x in range(10) if x % 2 == 0]
print(evens) # Output: [0, 2, 4, 6, 8]
Using Conditions (if)
You can filter items using an if
condition:
names = ["John", "Sara", "Paul", "Anna"]
short_names = [name for name in names if len(name) <= 4]
Nested List Comprehensions
You can nest comprehensions to handle nested data, like a 2D matrix:
matrix = [[1, 2], [3, 4], [5, 6]]
flattened = [num for row in matrix for num in row]
print(flattened) # Output: [1, 2, 3, 4, 5, 6]
With Functions
You can call functions inside list comprehensions:
def square(x):
return x * x
results = [square(x) for x in range(5)]
Dictionary and Set Comprehensions (Bonus)
List comprehensions have siblings!
Dictionary Comprehension:
squares = {x: x*x for x in range(5)}
# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Set Comprehension:
unique_lengths = {len(word) for word in ["apple", "banana", "cherry"]}
# Output: {5, 6}
Common Use Cases
-
Filtering even/odd numbers
-
Removing duplicates from a list
-
Flattening nested lists
-
Mapping transformations (e.g. converting strings to lowercase)
-
Reading and processing file lines
List Comprehensions vs Loops
Feature | For Loop | List Comprehension |
---|---|---|
Verbosity | More lines of code | One-liner |
Readability | Clearer for complex logic | Clearer for simple operations |
Performance | Slightly slower in some cases | Often faster |
Rule of thumb:
Use comprehensions when the logic is simple and readable. For complex logic or side-effects (like print/log), stick with loops.
Common Mistakes to Avoid
-
Trying to do too much in one line → hurts readability
-
Forgetting to include
if
condition at the right place -
Using nested comprehensions without understanding them
Practice Problems
Try writing list comprehensions for the following:
-
Get all numbers divisible by 3 from 0 to 50
-
Convert all strings in a list to uppercase
-
Extract first letters of each word in a sentence
Conclusion
List comprehensions are an elegant and Pythonic way to write clean, concise code. Once you get used to them, you’ll find they make many common tasks simpler and more readable.
Explore them in your projects, and when in doubt—test both list comprehension and the regular loop to see what’s clearer.
For more Python tutorials, check out our posts on: