List Comprehensions
📚 What are List Comprehensions? A list comprehension is a compact, readable way to build a new list from another list in a single line. Instead of writing a for loop, appending each item, you write: new_list = [expression for item in old_list]. You can add a filter with an if at the end.

Appy Says…
Want to filter a list of users, double every number, or extract all the hashtags from posts? Normally that's a 4-line for loop. With list comprehensions you do it in ONE line — and it's faster too. This is the feature that makes Python feel like a superpower.
What is a List Comprehension?
A list comprehension is a compact way to create a new list by applying an expression to each item in an existing iterable, optionally filtering items with a condition.
- •Basic syntax:
[expression for item in iterable] - •With filter:
[expression for item in iterable if condition] - •Example:
[x * 2 for x in range(5)]→[0, 2, 4, 6, 8] - •Example:
[name for name in users if name.startswith('A')] - •Can call functions:
[score.upper() for score in grades] - •Nested (use sparingly):
[x+y for x in [1,2] for y in [3,4]]
Think of it like a Minecraft item filter
Imagine a chest full of Minecraft items. A list comprehension is like a hopper with a filter — it picks up every item, checks if it matches your rule, and drops matching items into a new sorted chest. All in one automatic flow.
How It Works
- •1. Python reads the comprehension right-to-left: first the
for, then theif, then the expression - •2. It iterates through every item in the iterable
- •3. For each item, if a condition is given, it checks it — skips if
False - •4. It applies the expression to passing items and adds the result to the new list
- •5. The whole thing builds and returns a brand new list
- •6. Equivalent to a for-loop +
.append(), but faster and more readable
Real-World Examples
- •TikTok filters trending videos:
[v for v in videos if v.views > 1_000_000] - •Spotify extracts song titles:
[song['title'] for song in playlist] - •A school app picks failing students:
[s for s in students if s.grade < 50] - •A weather app converts Celsius to Fahrenheit:
[c * 9/5 + 32 for c in temps] - •YouTube extracts hashtags:
[w for w in caption.split() if w.startswith('#')]
Key Facts
- •List comprehensions are typically 30–50% faster than equivalent for-loops in Python
- •Python also has dict comprehensions:
{k: v for k, v in pairs}and set comprehensions - •Generator expressions look the same but use
()— they save memory by producing items one at a time - •PEP 8 (Python's style guide) recommends keeping comprehensions on one line if they fit
Watch Out!
List comprehensions can become unreadable when nested more than two levels deep. If your comprehension takes more than 79 characters or has two nested for loops, switch back to a plain for-loop with descriptive variable names — clarity beats cleverness.
Remember
Read a comprehension right-to-left: [expression for item in iterable if condition]. Once it clicks, you'll use this pattern every day.
What You Learned
- •List comprehensions create new lists in one readable line:
[expr for x in iterable if cond] - •They're faster than for-loops and work with any iterable
- •Unlocks: data filtering, transformation pipelines, clean functional-style Python
Key Facts
- →List comprehensions are typically 30–50% faster than equivalent for-loops in Python
- →Python also has dict comprehensions:
{k: v for k, v in pairs}and set comprehensions - →Generator expressions look the same but use
()— they save memory by producing items one at a time - →PEP 8 (Python's style guide) recommends keeping comprehensions on one line if they fit
Real-World Examples
Remember
Read a comprehension right-to-left: [expression for item in iterable if condition]. Once it clicks, you'll use this pattern every day.
Quick Quiz
[x*2 for x in [1,2,3]] gives?