squares = []
for x in range(1, 6):
squares.append(x**2)
print(squares)[1, 4, 9, 16, 25]
(CSE331) Python for Data Science
A comprehension builds a collection by transforming or filtering items from an iterable. It is useful when the operation is short enough to read easily.
Start with a familiar loop:
squares = []
for x in range(1, 6):
squares.append(x**2)
print(squares)[1, 4, 9, 16, 25]
The equivalent list comprehension is:
squares = [x**2 for x in range(1, 6)]
print(squares)[1, 4, 9, 16, 25]
Read this as: “calculate x**2 for each x in the range.” A list comprehension immediately constructs the whole result list.
Put a filtering condition after the for clause:
evens = [x for x in range(1, 11) if x % 2 == 0]
print(evens)[2, 4, 6, 8, 10]
General form:
[expression for item in iterable if condition]Only items satisfying the condition contribute to the result.
A conditional expression goes before the for clause:
numbers = [-3, 0, 5, -1]
nonnegative = [x if x >= 0 else 0 for x in numbers]
print(nonnegative)[0, 0, 5, 0]
This keeps the original length and replaces negative values with zero. Compare:
filtered = [x for x in numbers if x >= 0]
print(filtered)[0, 5]
| Form | Purpose |
|---|---|
[x for x in values if condition] |
Keep selected items |
[a if condition else b for x in values] |
Choose a result for every item |
squares_by_number = {x: x**2 for x in range(1, 6)}
print(squares_by_number){1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
If a key occurs more than once, its later value replaces the earlier one.
vowels = {ch for ch in "statistics" if ch in "aeiou"}
print(sorted(vowels)) # sorted display for reproducible output['a', 'i']
Sets keep unique elements. Dictionaries and sets are collections, but they are not Python sequence types.
pairs = [(x, y) for x in range(1, 4) for y in range(1, 4)]
print(pairs)[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
These are coordinate pairs, not multiplication results. The order matches these loops:
pairs_loop = []
for x in range(1, 4):
for y in range(1, 4):
pairs_loop.append((x, y))
print(pairs_loop == pairs)True
A generator expression produces results as needed, rather than building a list immediately.
squares_gen = (x**2 for x in range(1, 6))
print(next(squares_gen)) # 1
print(next(squares_gen)) # 4
print(list(squares_gen)) # [9, 16, 25]
print(list(squares_gen)) # []: already exhausted1
4
[9, 16, 25]
[]
The generator is consumed as values are requested. Create a new generator to start again. This is the same behaviour seen with generator functions in Lecture 4.
Parentheses here create a generator expression, not a tuple.
print() repeatedly.values = [-2, 5, 0, -7, 4], first keep only nonnegative values, then write a separate comprehension that replaces negative values with zero. Explain why the lengths differ."statistics".for clauses to generate all pairs (x, y) where x is 1 or 2 and y is 1, 2, or 3.next() twice, then display the remaining values using list().if filters items; an if/else expression selects a value.for clauses matches the equivalent nested loops.