Course Progress36%
🍎 Python Basics Topic 36 / 100
⏳ 8 min read

List Comprehensions

Build lists in one elegant line — Python's most loved shortcut for transforming and filtering collections.

"A list comprehension replaces a 3-line loop with one readable line. Once you see it click, you'll use it everywhere."

— ShurAI

The Problem: Building Lists with Loops

The traditional way to build a new list from an old one takes 3 lines. A list comprehension does the same in 1:

python — old way vs comprehension
# Old way — 3 lines
squares = []
for n in range(1, 6):
    squares.append(n * n)

# List comprehension — 1 line, same result
squares = [n * n for n in range(1, 6)]

print(squares)   # [1, 4, 9, 16, 25]

Anatomy of a List Comprehension

[ expression for item in iterable ]
What to put in the new list
(can transform item)
Loop variable
The source list or range

Basic Examples

python
# Double each number
doubled = [x * 2 for x in [1, 2, 3, 4, 5]]
print(doubled)    # [2, 4, 6, 8, 10]

# Uppercase all fruits
fruits = ["apple", "mango", "guava"]
upper  = [f.upper() for f in fruits]
print(upper)      # ['APPLE', 'MANGO', 'GUAVA']

# Get length of each word
words   = ["hi", "python", "code"]
lengths = [len(w) for w in words]
print(lengths)    # [2, 6, 4]

With a Condition — Filtering

Add if condition at the end to include only items that pass the test:

python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Only even numbers
evens = [n for n in nums if n % 2 == 0]
print(evens)     # [2, 4, 6, 8, 10]

# Scores above 70 only
scores  = [45, 82, 67, 91, 55, 78]
passing = [s for s in scores if s >= 70]
print(passing)   # [82, 91, 78]

# Words longer than 3 letters
words = ["hi", "python", "is", "cool", "ok"]
long  = [w for w in words if len(w) > 3]
print(long)      # ['python', 'cool']

Transform AND Filter Together

python
# Square only the even numbers
result = [n**2 for n in range(10) if n % 2 == 0]
print(result)   # [0, 4, 16, 36, 64]

# Uppercase names that start with R
names  = ["Riya", "Arjun", "Rekha", "Sneha"]
r_names = [n.upper() for n in names if n.startswith("R")]
print(r_names)  # ['RIYA', 'REKHA']

Real Example — Clean a Dataset

python
# Raw data with messy strings and invalid scores
raw_names  = ["  riya  ", "ARJUN", " sneha", "vikram  "]
raw_scores = [88, -5, 92, 105, 76, 0]

# Clean names: strip whitespace, title case
clean_names = [n.strip().title() for n in raw_names]
print(clean_names)   # ['Riya', 'Arjun', 'Sneha', 'Vikram']

# Valid scores only (0-100)
valid_scores = [s for s in raw_scores if 0 <= s <= 100]
print(valid_scores)  # [88, 92, 76, 0]
When NOT to use a comprehension

If the logic is complex or needs multiple lines, use a regular loop — readability comes first. A comprehension with three nested conditions is harder to read than a simple loop. The goal is clarity, not cleverness.

"The best comprehension is one that any programmer can read in 3 seconds and understand immediately. If it takes longer, write a loop."

— ShurAI

🧠 Quiz — Q1

What does [x * 2 for x in [1, 2, 3]] produce?

🧠 Quiz — Q2

What does the if part in a list comprehension do?

🧠 Quiz — Q3

What does [n for n in range(6) if n % 2 == 0] produce?

🧠 Quiz — Q4

A list comprehension is always faster to write than a loop. When should you prefer a regular loop instead?