A list comprehension is a short way to build a new Python list from another iterable.
The easiest way to understand it is to start with a normal for loop and then shorten that same idea.
Start with a normal loop
numbers = [1, 2, 3, 4]
squares = []
for number in numbers:
squares.append(number * number)
print(squares)
Output:
[1, 4, 9, 16]
This code says: take each number, square it, and add the result to a new list.
Now write the same idea as a list comprehension
numbers = [1, 2, 3, 4]
squares = [number * number for number in numbers]
print(squares)
The output is still:
[1, 4, 9, 16]
Nothing magical happened. Python simply gives us a compact form for this common pattern.
Read it in three pieces
[number * number for number in numbers]
└──── result ───┘ └─ loop ─┘ └ source ┘
number * number— the value to put in the new list.for number— take one item at a time.in numbers— get those items fromnumbers.
If the one-line form feels confusing, write the normal loop first. Once the loop makes sense, the comprehension is much easier to read.
Video 1: List comprehensions in a few minutes
You can also copy items into a new list
players = ["Ava", "Jay", "Mia"]
new_players = [player for player in players]
print(new_players)
Output:
['Ava', 'Jay', 'Mia']
This example does not change each item. It simply shows the structure in its easiest form.
Transform each item
A comprehension becomes more useful when the new list needs a changed version of each item.
prices = [10, 20, 30]
doubled = [price * 2 for price in prices]
print(doubled)
Output:
[20, 40, 60]
The source list stays [10, 20, 30]. The comprehension creates a new list containing the calculated values.
Video 2: Making list comprehensions easier to read
Add one simple filter
You can add an if condition when only some items should enter the new list.
numbers = [1, 2, 3, 4, 5, 6]
evens = [number for number in numbers if number % 2 == 0]
print(evens)
Output:
[2, 4, 6]
Read it like this: put the number in the new list for each number in numbers if that number is even.
Gaming example
Suppose a game has player scores and we only want scores of 100 or higher.
scores = [55, 120, 88, 150, 40]
high_scores = [score for score in scores if score >= 100]
print(high_scores)
Output:
[120, 150]
The comprehension creates a new list containing only the scores that pass the condition.
Video 3: Building lists with comprehensions
How this connects to earlier Python lessons
OSPython.002: Lists, Tuples, and Sets introduced lists. OSPython.004: For Loops, Range, and Iteration introduced the loop used inside a comprehension. OSPython.019: Generators and yield Basics introduced another way Python can produce values through iteration.
When should you use the normal loop instead?
A list comprehension is useful when the operation is short and easy to understand. If the logic needs many steps, several conditions, logging, error handling, or other side effects, a normal loop is often clearer.
Shorter code is not automatically better code. The goal is code that another person can understand.
Common beginner mistakes
- Forgetting the square brackets
[ ]. - Putting the pieces in the wrong order.
- Trying to squeeze complicated multi-step logic into one line.
- Assuming a comprehension changes the original list when it actually creates a new list in these examples.
- Adding an
iffilter before understanding the basic no-filter form.
Quick practice
- Start with
numbers = [1, 2, 3, 4]. - Use a normal loop to create a list containing each number multiplied by 10.
- Rewrite that loop as a list comprehension.
- Create another comprehension that keeps only numbers greater than 2.
- Explain which part is the result expression, which part is the loop, and which part is the source.
Key takeaway
A list comprehension is a compact way to build a new list. Learn it by comparing it with the normal for loop first. Keep the one-line form when it stays easy to read; use a normal loop when the job becomes complicated.

Leave a comment