List Comprehensions in Python Explained
A list comprehension is a short way to create a new list in Python.
It lets you:
- loop through values
- optionally filter them
- build a new list in one expression
List comprehensions are useful when you want to transform or filter data in a clear, compact way. They are best for simple cases. If the logic becomes hard to read, a normal for loop is usually the better choice.
Quick example
numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers]
print(squares)
Output:
[1, 4, 9, 16, 25]
Use a list comprehension when you want to build a new list from another iterable in one clear line.
What a list comprehension is
A list comprehension is:
- a short way to create a new list
- a combination of looping and optional filtering
- something that returns a real list object
- best for simple transformations and simple filtering
For example, if you want a list of doubled numbers, you can build it directly:
numbers = [1, 2, 3]
doubled = [n * 2 for n in numbers]
print(doubled)
Output:
[2, 4, 6]
If you are new to lists, see Python lists explained.
Basic syntax
The basic pattern is:
[expression for item in iterable]
Here is what each part means:
expressionis the value that gets added to the new listitemis each value taken one at a timeiterableis the thing you loop over
An iterable can be:
- a list
- a tuple
- a string
- a
range() - other objects you can loop through
Example:
numbers = [1, 2, 3, 4]
result = [n + 1 for n in numbers]
print(result)
Output:
[2, 3, 4, 5]
In this example:
numbersis the iterablenis each itemn + 1is the expression
If range() is new to you, read Python range() function explained.
Compare with a normal for loop
It helps to see the longer version first.
Normal for loop
numbers = [1, 2, 3, 4]
squares = []
for n in numbers:
squares.append(n * n)
print(squares)
Output:
[1, 4, 9, 16]
Matching list comprehension
numbers = [1, 2, 3, 4]
squares = [n * n for n in numbers]
print(squares)
Output:
[1, 4, 9, 16]
Both versions produce the same result.
The list comprehension is shorter, but shorter is not always better. Use it when it stays easy to read. If you need help with loops first, see Python for loops explained.
Using conditions
You can add a condition to keep only some items.
The pattern is:
[expression for item in iterable if condition]
The if part filters the items before they are added to the new list.
Example: keep only even numbers
numbers = range(10)
evens = [n for n in numbers if n % 2 == 0]
print(evens)
Output:
[0, 2, 4, 6, 8]
Example: keep only non-empty strings
words = ["apple", "", "banana", "", "grape"]
non_empty = [word for word in words if word != ""]
print(non_empty)
Output:
['apple', 'banana', 'grape']
Example: keep only positive values
values = [-2, 5, 0, 7, -1]
positive = [v for v in values if v > 0]
print(positive)
Output:
[5, 7]
If you want more filtering examples, see how to filter a list in Python.
Changing each item
The expression part is where you change each value.
This is useful for simple one-step transformations.
Square numbers
numbers = [1, 2, 3, 4]
squares = [n * n for n in numbers]
print(squares)
Output:
[1, 4, 9, 16]
Convert strings to lowercase
words = ["PYTHON", "List", "Comprehension"]
lowercase_words = [word.lower() for word in words]
print(lowercase_words)
Output:
['python', 'list', 'comprehension']
Add 1 to each number
numbers = [10, 20, 30]
result = [n + 1 for n in numbers]
print(result)
Output:
[11, 21, 31]
These examples are simple and readable. That is the best use case for list comprehensions.
Reading list comprehensions step by step
A list comprehension can look confusing at first. A good way to read it is in this order:
- Start with the
forpart
Ask: where do the values come from? - Check the
ifpart, if there is one
Ask: which values are kept? - Read the expression at the beginning
Ask: what gets added to the new list?
Example:
result = [n * 2 for n in range(6) if n % 2 == 0]
print(result)
Read it step by step:
for n in range(6)means values come from0to5if n % 2 == 0means keep only even numbersn * 2means double each remaining number
Output:
[0, 4, 8]
If a comprehension feels hard to read, rewrite it as a normal loop. That is a good habit for beginners.
When to use them and when not to
Use list comprehensions when:
- you are building a new list
- the task is short and clear
- you are doing a simple transformation
- you are doing simple filtering
Do not use them when:
- the logic is hard to read
- you need many steps
- you need complex
iflogic - a nested list comprehension makes the code confusing
Good use
names = ["Ana", "Ben", "Cara"]
lengths = [len(name) for name in names]
print(lengths)
Output:
[3, 3, 4]
Better as a normal loop
numbers = [1, 2, 3, 4, 5]
results = []
for n in numbers:
if n % 2 == 0:
value = n * 10
results.append(value)
print(results)
Output:
[20, 40]
You could write that in one line, but a normal loop may be easier to understand.
For more practice-focused examples, see how to use list comprehensions in Python.
Common beginner mistakes
Here are some common problems beginners run into.
Forgetting the brackets
A list comprehension must use square brackets.
Incorrect:
n * n for n in [1, 2, 3]
Correct:
[n * n for n in [1, 2, 3]]
Putting parts in the wrong order
The order is:
- expression
for- optional
if
Correct:
[n * 2 for n in [1, 2, 3] if n > 1]
Incorrect:
[for n in [1, 2, 3] n * 2]
Using print() inside the comprehension
If you want to build a list, the expression should return values to store.
This is usually not useful:
numbers = [1, 2, 3]
result = [print(n) for n in numbers]
print(result)
Output:
1
2
3
[None, None, None]
print() prints to the screen, but it returns None. So the list fills with None values.
Writing code that is too complex for one line
If you need to stop and decode the line slowly, a normal loop is probably better.
This is one reason beginners confuse list comprehensions with normal loops. The syntax is similar, but the order is different:
- normal loop: start with
for - list comprehension: start with the expression
FAQ
What is a list comprehension in Python?
It is a short way to create a new list from another iterable, with optional filtering.
Are list comprehensions faster than for loops?
They are often concise and can be efficient, but beginners should focus first on readability.
Can I use if in a list comprehension?
Yes. You can filter items with:
[value for item in iterable if condition]
Should beginners use list comprehensions?
Yes, for simple cases. If the line becomes hard to read, use a normal for loop instead.