Lesson 3 of 5 · 6 min · Working with data
Comprehensions: building lists, dicts and sets in one line
What you will learn
- List, dict and set comprehensions
- Filtering and transforming in one expression
- Generator expressions and why they save memory
- When to use a plain loop instead
In simple words
A comprehension is a short way to say "make a new collection from an old one". Instead of creating an empty list and adding to it one item at a time, you describe the new list in a single line: take each item, maybe change it, maybe skip some. It is like writing "squares of 1 to 5" instead of listing every step.
Words to know
- Comprehension: a one-line expression that builds a list, dict or set from a loop.
- Iterable: anything you can loop over, such as a list, string or file.
- Generator: an object that produces values one at a time instead of building the whole list.
- Filter: keeping only the items that pass a test.
List comprehensions
The loop way:
squares = []
for n in range(1, 6):
squares.append(n * n)
The comprehension way:
squares = [n * n for n in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
The pattern is [expression for item in iterable]. Add if at the end to filter:
prices = [120, 450, 80, 999, 300]
expensive = [p for p in prices if p > 300]
print(expensive) # [450, 999]
You can transform and filter together:
names = [" asha ", "RAVI", "", "meena"]
clean = [n.strip().title() for n in names if n.strip()]
print(clean) # ['Asha', 'Ravi', 'Meena']
An if/else that chooses a value goes before the for, because it is part of the expression:
labels = ["high" if p > 300 else "low" for p in prices]
Dict and set comprehensions
Use braces. A dict comprehension has key: value:
lengths = {name: len(name) for name in ["sql", "excel", "python"]}
print(lengths) # {'sql': 3, 'excel': 5, 'python': 6}
A set comprehension removes duplicates:
cities = ["Hyderabad", "hyderabad", "Pune", "PUNE"]
unique = {c.lower() for c in cities}
print(unique) # {'hyderabad', 'pune'} (order may vary)
A common analyst pattern is turning a list of dicts into a lookup:
rows = [{"id": 1, "name": "Asha"}, {"id": 2, "name": "Ravi"}]
by_id = {r["id"]: r["name"] for r in rows}
print(by_id[2]) # Ravi
Nested loops and flattening
Loops in a comprehension read in the same order as written loops:
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [x for row in matrix for x in row]
print(flat) # [1, 2, 3, 4, 5, 6]
Keep it to two loops at most. Beyond that, a normal loop is easier to read.
Generator expressions
Use parentheses to get a generator, which produces one value at a time and never builds the full list:
total = sum(n * n for n in range(1_000_000))
This uses almost no memory, because sum pulls one square at a time. A list comprehension would build a million-item list first. Use a generator expression when you only need to loop over the result once, such as feeding sum, max, any or all:
any(p > 900 for p in prices) # True
A generator can be used only once; after it is exhausted it gives nothing.
Useful helpers
enumerate(items)gives(index, item)pairs:[f"{i}. {s}" for i, s in enumerate(skills, start=1)].zip(a, b)pairs two lists:dict(zip(names, ages)).sorted(items, key=..., reverse=True)returns a new sorted list.
When not to use one
If the comprehension needs several conditions, side effects (printing, writing files) or long expressions, use a loop. Comprehensions are for building a collection, not for doing work. Readability wins.
Common mistakes
- Using a comprehension for side effects, such as
[print(x) for x in items]. - Putting
if/elseafter thefor(that is a syntax error); a filter-onlyifgoes after, a choosingif/elsegoes before. - Reusing a generator after it is exhausted.
- Cramming three nested loops into one line.
Try it yourself
- Given
[3, 8, 12, 5, 20], build the list of numbers above 7, doubled. - Build a dict mapping each word in a sentence to its length.
- Flatten
[[1, 2], [3], [4, 5]]with a comprehension. - Compute
sumof squares of 1 to 100 with a generator expression and compare with a list comprehension.
Check yourself
What does [x for x in range(10) if x % 2 == 0] give?
[0, 2, 4, 6, 8], the even numbers below 10.
What is the difference between [x for x in data] and (x for x in data)?
The first builds a whole list in memory. The second is a generator expression that produces items one at a time.
Where does an if/else go when choosing between two values?
Before the for: [a if cond else b for item in items].
How do you remove duplicates while transforming?
Use a set comprehension, {f(x) for x in items}.
Interview questions
When would you choose a generator over a list?
When the data is large or you only need to loop once. A generator keeps memory use small and can start producing results immediately.
Why can't you iterate a generator twice?
It holds only its current position and produces each value once. After the last value it is exhausted; create a new generator to start again.
Rewrite a loop that filters and transforms into a comprehension.
result = []; for x in items: if x.ok: result.append(x.value) becomes result = [x.value for x in items if x.ok].
Summary
[expr for item in iterable if condition]builds a list; braces make dicts and sets.- Parentheses make a lazy generator, ideal for
sum,max,anyandall. - Keep comprehensions short; use a loop when they get complicated.
Going deeper
- List comprehensions: the official tutorial section.
- Displays for lists, sets and dictionaries: the exact syntax.
- Generator expressions: reference.
See how much of this you can explain
A free mock interview asks you new questions on Python essentials for analysts for Data Analyst and scores every answer, with what to fix.