SyntaxPilot
Courses/Python/Lesson 16

Python · Lesson 16 of 40 · 10 min

List, dict and set comprehensions

queued

These are the notes for Python episode 16, the first episode of the intermediate section. You already know loops (episodes 7-9), lists (10), tuples and sets (11) and dictionaries (12). In this episode you notice that you keep writing the same three lines (an empty list, a for, an append) and invent a shorter way: the comprehension. You will learn why changing the loop variable doesn't change a list, list comprehensions, filtering with if, choosing a value with if/else (and the SyntaxError almost everyone hits), zip() and dictionary comprehensions, set comprehensions, nested comprehensions and flattening, why round brackets give a generator instead of a tuple, and when a plain loop is the better choice. Every example below runs as-is in the SyntaxPilot compiler, with its real output from Python 3.13.

The problem: five grace marks for the whole class

The maths paper was too hard, so the principal says: give every student five grace marks. On paper, you go down the register and rewrite each mark. In Python, the obvious attempt is a for loop that adds 5 to each mark:

marks = [35, 72, 48, 90, 38]

for m in marks:
    m = m + 5

print(marks)

Output:

[35, 72, 48, 90, 38]

Nothing changed, and there is no error message, which is worse than a crash: the program silently gives the wrong answer.

Why: m is only a name for each value, one at a time. m = m + 5 makes the name m point at a new number (40); the list's slot still holds the old number (35). The loop variable is not the list slot.

There are two ways to really change the marks:

marks = [35, 72]
for i in range(len(marks)):
    marks[i] = marks[i] + 5
print(marks)

Output:

[40, 77]

This changes the list in place, through its index. More often, you want a new list and to keep the original. The version that works:

marks = [35, 72, 48, 90, 38]

new_marks = []
for m in marks:
    new_marks.append(m + 5)

print(new_marks)

Output:

[40, 77, 53, 95, 43]

It works, but it takes three lines (an empty list, a for, an append) to say one idea: "every mark plus five".

The pattern that keeps coming back

You will write this shape hundreds of times: prices with GST, names in capitals, marks as percentages.

new = []
for m in marks:
    new.append(m + 5)

Only two things change each time:

Part In the example Meaning
what to put in m + 5 the value for each item of the new list
where it comes from for m in marks the loop over the old collection

So Python lets you write just those two things inside square brackets: [m + 5 for m in marks]. That is a list comprehension.

List comprehensions

marks = [35, 72, 48, 90, 38]

new_marks = [m + 5 for m in marks]
print(new_marks)

print([m * 2 for m in marks])
print([str(m) + "%" for m in marks])

Output:

[40, 77, 53, 95, 43]
[70, 144, 96, 180, 76]
['35%', '72%', '48%', '90%', '38%']

Read it like English: "m + 5, for every m in marks". The general shape:

[expression for item in iterable]
  • expression: what each new item should be (it can use item).
  • item: the loop variable, a name for each element in turn.
  • iterable: anything you can loop over: a list, a string, a range(), a tuple, a set, a dictionary.

A comprehension always builds a brand new list. The original marks list is untouched.

print([c for c in "chai"])
print([n * n for n in range(1, 6)])

Output:

['c', 'h', 'a', 'i']
[1, 4, 9, 16, 25]

Filtering with if

The teacher now wants only the students who passed (40 or more). In a loop, you'd put an if before the append. In a comprehension, the if goes at the end:

marks = [35, 72, 48, 90, 38]

passed = [m for m in marks if m >= 40]
print(passed)
print(len(passed), "students passed")

Output:

[72, 48, 90]
3 students passed

The if at the end is a filter: an item that fails the test is simply left out. The loop version, for comparison:

passed = []
for m in marks:
    if m >= 40:
        passed.append(m)

Choosing a value with if/else

Now the teacher wants a word for every student: "pass" or "fail". That's a different job.

Real life Code Result length
Filter a sieve: only some papers get through [m for m in marks if m >= 40] (the if at the end) fewer items
Choice a stamp on every paper: PASS or FAIL ["pass" if m >= 40 else "fail" for m in marks] (if/else in front) the same number of items

The obvious attempt is to add else to the filter:

marks = [35, 72, 48, 90, 38]

result = [m for m in marks if m >= 40 else 0]
print(result)

Output:

  File "main.py", line 3
    result = [m for m in marks if m >= 40 else 0]
                                          ^^^^
SyntaxError: invalid syntax

Why: the if at the end is a sieve. A sieve can't have an else; it either keeps an item or drops it. To choose a value for every item, the choice is part of the expression, so it goes in front of the for, where the value goes. It uses Python's conditional expression A if condition else B (a one-line if/else that gives a value):

marks = [35, 72, 48, 90, 38]

result = ["pass" if m >= 40 else "fail" for m in marks]
print(result)

Output:

['fail', 'pass', 'pass', 'pass', 'fail']

You can use both at once: choose a value and filter.

temps = [31, 35, 29, 38, 40, 33]
print(["hot" if t >= 35 else "ok" for t in temps])
print([t for t in temps if t >= 35])

Output:

['ok', 'hot', 'ok', 'hot', 'hot', 'ok']
[35, 38, 40]

Remember: if alone at the end filters. if with else in front chooses.

zip(): pairing two lists

The register has two columns: names in one list, marks in another. They only match up by position. zip() is a built-in function that joins them like the zip on a jacket, tooth by tooth: the first name with the first mark, the second with the second, and so on.

names = ["Asha", "Ravi", "Meera", "Kabir", "Isha"]
marks = [35, 72, 48, 90, 38]

print(list(zip(names, marks)))

Output:

[('Asha', 35), ('Ravi', 72), ('Meera', 48), ('Kabir', 90), ('Isha', 38)]

Each pair is a tuple (episode 11). zip() is lazy (it hands out one pair at a time), so we wrap it in list() to see all the pairs. If the lists have different lengths, zip() stops at the shorter one:

print(list(zip(["a", "b", "c"], [1, 2])))

Output:

[('a', 1), ('b', 2)]

Dictionary comprehensions

What we really want is a report card: look up a name, get the mark. That's a dictionary (episode 12). A dict comprehension uses curly brackets and key: value:

names = ["Asha", "Ravi", "Meera", "Kabir", "Isha"]
marks = [35, 72, 48, 90, 38]

report = {name: mark for name, mark in zip(names, marks)}
print(report)
print(report["Meera"])

toppers = {name: mark for name, mark in report.items() if mark >= 70}
print(toppers)

Output:

{'Asha': 35, 'Ravi': 72, 'Meera': 48, 'Kabir': 90, 'Isha': 38}
48
{'Ravi': 72, 'Kabir': 90}

for name, mark in zip(...) unpacks each pair into two names. A filter works here too. Shape:

{key_expression: value_expression for item in iterable if condition}

Swapping keys and values is a classic one-liner:

d = {"a": 1, "b": 2}
print({v: k for k, v in d.items()})

Output:

{1: 'a', 2: 'b'}

Set comprehensions

A chai stall's orders for one minute. Which items were ordered at all? Curly brackets without a colon build a set (episode 11): no duplicates.

orders = ["chai", "samosa", "chai", "vada pav", "chai", "samosa"]

items = {o for o in orders}
print(items)

counts = {o: orders.count(o) for o in items}
print(counts)

Output (a set has no fixed order, so yours may differ):

{'samosa', 'chai', 'vada pav'}
{'samosa': 2, 'chai': 3, 'vada pav': 1}

Which bracket builds what

Brackets Example Builds
[ ... for ... ] [m + 5 for m in marks] a list
{ k: v for ... } {n: m for n, m in zip(names, marks)} a dict
{ ... for ... } {o for o in orders} a set
( ... for ... ) (n * n for n in range(5)) a generator, NOT a tuple

Nested comprehensions and flattening

A comprehension can contain another comprehension, or have two fors. A 3×3 times table:

table = [[r * c for c in range(1, 4)] for r in range(1, 4)]
print(table)

flat = [x for row in table for x in row]
print(flat)

Output:

[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
[1, 2, 3, 2, 4, 6, 3, 6, 9]
  • [[r * c for c in ...] for r in ...]: the inside comprehension builds one row; the outside one builds a row for each r.
  • [x for row in table for x in row]: two fors, read left to right, exactly like nested loops (episode 9). The first for is the outer loop:
flat = []
for row in table:
    for x in row:
        flat.append(x)

Round brackets: a generator, not a tuple

squares = (n * n for n in range(5))
print(squares)
print(list(squares))

Output (the address differs on each run):

<generator object <genexpr> at 0x7f76cfd63440>
[0, 1, 4, 9, 16]

Round brackets build a generator expression: a lazy recipe that hands out one value at a time, only when asked. It saves memory for huge sequences (sum(n * n for n in range(1_000_000)) never builds a million-item list). Generators get their own episode (24). If you want a tuple, say so: tuple(n * n for n in range(5)).

The loop variable stays inside

total = [x for x in range(3)]
print(total)
print(x)

Output:

[0, 1, 2]
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(x)
          ^
NameError: name 'x' is not defined

The comprehension's loop variable exists only inside it, so a comprehension can't accidentally overwrite a variable of yours. (A normal for loop's variable does stay after the loop.)

When NOT to use a comprehension

Comprehensions are great for one simple idea. Squeeze two fors and three ifs into one, and nobody can read it:

r = [x * y for x in a if x > 0 for y in b if y % 2 == 0 if x != y]

The same logic as a loop is longer, but clear:

r = []
for x in a:
    if x > 0:
        for y in b:
            if y % 2 == 0 and x != y:
                r.append(x * y)

Rules of thumb:

  • If it doesn't fit comfortably on one line, write a loop.
  • If you only want to do something (print, save, send) and not build a collection, write a loop. [print(m) for m in marks] builds a useless list of Nones.
  • Comprehensions are usually a little faster than the equivalent append loop, but choose them for readability, not speed.

Common mistakes

Mistake What you see Why Fix
for m in marks: m = m + 5 no error, the list is unchanged m is only a name for each value [m + 5 for m in marks] (new list) or marks[i] = ...
[m for m in marks if m >= 40 else 0] SyntaxError: invalid syntax a filter if can't have an else [m if m >= 40 else 0 for m in marks]
(n * n for n in range(5)) expecting a tuple <generator object <genexpr> at 0x...> round brackets make a generator tuple(n * n for n in range(5))
print(x) after [x for x in ...] NameError: name 'x' is not defined the loop variable stays inside use the list you built
{o for o in orders} expecting a fixed order the order changes between runs sets have no order sorted({...}) or a dict
[print(m) for m in marks] a list of Nones used for its side effect a normal for loop
two fors in the wrong order: [x for x in row for row in table] NameError: name 'row' is not defined. Did you mean: 'pow'? the outer loop must come first [x for row in table for x in row]

Mini project: a chai stall's day

A menu dictionary of prices and a list of the day's orders. Three questions, three comprehensions: the price of every order, how many of each item were sold, and which items cost ₹25 or more.

# Mini project: a chai stall's day
menu = {"chai": 15, "samosa": 20, "vada pav": 25, "coffee": 30}
orders = ["chai", "samosa", "chai", "vada pav", "chai", "coffee", "samosa"]

bill = [menu[o] for o in orders]
sold = {item: orders.count(item) for item in menu}
costly = [item for item, price in menu.items() if price >= 25]

print("Bill:", bill, "Total:", sum(bill))
print("Sold:", sold)
print("Costly items:", costly)

Output:

Bill: [15, 20, 15, 25, 15, 30, 20] Total: 140
Sold: {'chai': 3, 'samosa': 2, 'vada pav': 1, 'coffee': 1}
Costly items: ['vada pav', 'coffee']
  • bill looks up each order's price (a list, one entry per order).
  • sold loops over the menu (not a set), so the order is fixed and every menu item appears.
  • costly filters the menu's (item, price) pairs.

Extend it: list the items that sold zero times (practice 4).

Practice

1. The squares of 1 to 20 that are odd.

Solution
print([n * n for n in range(1, 21) if n % 2 == 1])

Output:

[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]

2. A dictionary of word → its length, for a sentence.

Solution
sentence = "chai is the best drink"
lengths = {word: len(word) for word in sentence.split()}
print(lengths)

Output:

{'chai': 4, 'is': 2, 'the': 3, 'best': 4, 'drink': 5}

3. The unique first letters of 10 names, in alphabetical order.

Solution
names = ["Asha", "Ravi", "Arjun", "Meera", "Rohit", "Isha", "Kabir", "Anil", "Mohan", "Priya"]
firsts = {name[0] for name in names}
print(sorted(firsts))

Output:

['A', 'I', 'K', 'M', 'P', 'R']

A set comprehension removes the repeats; sorted() gives a list in a fixed order.

4. Chai stall: the items that sold zero times (we added lassi to the menu so there is one).

Solution
menu = {"chai": 15, "samosa": 20, "vada pav": 25, "coffee": 30, "lassi": 35}
orders = ["chai", "samosa", "chai", "vada pav", "chai", "coffee", "samosa"]
unsold = [item for item in menu if item not in orders]
print(unsold)

Output:

['lassi']

5. Label each day's temperature "hot" (35 or more) or "ok", then list only the hot temperatures.

Solution
temps = [31, 35, 29, 38, 40, 33]
print(["hot" if t >= 35 else "ok" for t in temps])
print([t for t in temps if t >= 35])

Output:

['ok', 'hot', 'ok', 'hot', 'hot', 'ok']
[35, 38, 40]

The first is a choice (same length), the second a filter (shorter).

Quick quiz

1. The video's quiz:

nums = [1, 2, 3, 4, 5, 6]
print([n * 10 for n in nums if n % 2 == 0])

[10, 30, 50], [20, 40, 60], [10, 20, 30, 40, 50, 60], or SyntaxError?

Answer

[20, 40, 60]. The filter keeps only the even numbers (2, 4, 6), and each one is multiplied by 10.

2. What does this print?

print([c.upper() for c in "chai" if c != "a"])
Answer

['C', 'H', 'I']. A string is iterable, one character at a time; "a" is filtered out and the rest are capitalised.

3. What does this print?

print({x % 3 for x in range(10)})
Answer

{0, 1, 2}. Curly brackets without a colon build a set, so the ten remainders collapse to the three unique ones.

4. Which line is a SyntaxError? (a) [x if x > 0 else 0 for x in nums] (b) [x for x in nums if x > 0] (c) [x for x in nums if x > 0 else 0]

Answer

(c). A filter if at the end can't have an else. (a) is a choice, (b) a filter; both are valid.

Interview questions

  1. What is a list comprehension? A compact expression that builds a new list from an iterable: [expression for item in iterable if condition]. It replaces the "empty list, for, append" pattern.
  2. What's the difference between [x for x in a if c] and [x if c else y for x in a]? The first filters (the result can be shorter); the second chooses a value for every item using a conditional expression (same length).
  3. How do you build a dictionary or set with a comprehension? {k: v for ...} for a dict, {v for ...} for a set.
  4. What does (x for x in a) create? A generator expression: a lazy iterator, not a tuple. Use tuple(...) for a tuple.
  5. Does the loop variable of a comprehension leak into the surrounding scope? No (in Python 3), it's local to the comprehension.
  6. What does zip() do? Pairs up items from several iterables by position, stopping at the shortest.
  7. When would you avoid a comprehension? When the logic needs several conditions or nested loops that hurt readability, or when you only want side effects (printing, writing files).
  8. How do you flatten a list of lists? [x for row in matrix for x in row]: the fors are written in the same order as nested loops.

Glossary

Term Meaning Example
Comprehension A one-line expression that builds a new list, dict or set from an iterable [m + 5 for m in marks]
List comprehension A comprehension in square brackets; builds a list [n * n for n in range(5)]
Dict comprehension Curly brackets with key: value; builds a dictionary {n: m for n, m in zip(names, marks)}
Set comprehension Curly brackets without a colon; builds a set {o for o in orders}
Expression A piece of code that produces a value m + 5, "pass"
Iterable Anything you can loop over with for a list, string, range(), dict
Loop variable The name given to each element in turn m in for m in marks
Filter An if at the end that leaves out items failing a test if m >= 40
Conditional expression A one-line if/else that gives a value "pass" if m >= 40 else "fail"
zip() Pairs up items from several iterables by position zip(names, marks)
Unpacking Giving each part of a pair (tuple) its own name for name, mark in ...
Flattening Turning a list of lists into one flat list [x for row in t for x in row]
Nested comprehension A comprehension inside another, or with two fors a times table
Generator expression A comprehension in round brackets; a lazy recipe, not a tuple (episode 24) (n * n for n in range(5))
Lazy Produces values only when asked, one at a time zip(), generators
In place Changing the original object instead of making a new one marks[i] = marks[i] + 5
Side effect Something a line does besides producing a value, like printing print(m)
count() A list method: how many times a value appears orders.count("chai") → 3

Summary

  • Changing the loop variable (m = m + 5) doesn't change the list: m is just a name for each value.
  • A list comprehension [expression for item in iterable] replaces the "empty list, for, append" pattern and always builds a new list.
  • An if at the end is a filter; A if cond else B in front chooses a value for every item. if ... else at the end is a SyntaxError.
  • zip() pairs two lists by position; {k: v for ...} builds a dict, {v for ...} a set.
  • Two fors read left to right like nested loops; [x for row in t for x in row] flattens.
  • Round brackets give a generator, not a tuple; the loop variable doesn't leak out.
  • One idea per comprehension; otherwise use a loop.

Next episode: lambda, map, filter and sorted. How to sort students by marks instead of by name, with tiny one-line functions.