6 Comprehensions

(CSE331) Python for Data Science

Author
Affiliation

Md Rasel Biswas

IASDS, University of Dhaka

1 From Loops to Comprehensions

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.

2 Filtering Items

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.

3 Choosing a Value for Each Item

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

4 Dictionary and Set Comprehensions

4.1 Dictionary comprehension

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.

4.2 Set comprehension

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.

5 Comprehensions with Multiple Loops

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

6 Generator Expressions

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 exhausted
1
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.

7 Choosing a Clear Form

  • Use a comprehension for a short transformation or filter.
  • Use a loop when several statements, complex decisions, or intermediate explanations are needed.
  • Use comprehensions to construct results, not merely to call print() repeatedly.
  • A comprehension can be concise, but it is not automatically more readable or memory-efficient than a loop.
  • Comprehensions are still Python iteration. NumPy will introduce array operations designed for numerical calculations.

8 Exercises

  1. Create a list comprehension containing all integers divisible by 3 between 1 and 30 inclusive.
  2. For 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.
  3. Use a set comprehension to find the unique vowels in "statistics".
  4. Create a dictionary mapping integers 1 through 10 to their cubes.
  5. Use a comprehension with two for clauses to generate all pairs (x, y) where x is 1 or 2 and y is 1, 2, or 3.
  6. Use a generator expression to produce the cubes of integers 1 through 6. Call next() twice, then display the remaining values using list().

9 Summary

  • A trailing if filters items; an if/else expression selects a value.
  • List, set, and dictionary comprehensions construct different collection types.
  • The order of multiple for clauses matches the equivalent nested loops.
  • Generators are lazy and can be exhausted.

10 Further reading