Skip to content
  • Comprehensions · Python

Comprehensions

LearnViaNotes @learnvianotes· 2 min read

Comprehensions build lists, dicts and sets in one readable expression: what to produce, from where, and which items.

The shape

Python
squares = [x * x for x in range(6)]
evens = [x for x in range(10) if x % 2 == 0]
labels = ["even" if x % 2 == 0 else "odd" for x in range(4)]
print(squares)    # [0, 1, 4, 9, 16, 25]
print(evens)      # [0, 2, 4, 6, 8]
print(labels)     # ['even', 'odd', 'even', 'odd']

Dict & set comprehensions

Python
words = ["apple", "bat", "cherry"]
lengths = {w: len(w) for w in words}
inverted = {v: k for k, v in lengths.items()}
first_letters = {w[0] for w in words}
print(lengths)            # {'apple': 5, 'bat': 3, 'cherry': 6}
print(inverted[3])        # bat
print(sorted(first_letters))   # ['a', 'b', 'c']

Nested loops & flattening

Python
matrix = [[1, 2, 3], [4, 5, 6]]
flat = [x for row in matrix for x in row]          # outer loop first
transposed = [list(col) for col in zip(*matrix)]
pairs = [(i, j) for i in range(3) for j in range(i + 1, 3)]
print(flat)          # [1, 2, 3, 4, 5, 6]
print(transposed)    # [[1, 4], [2, 5], [3, 6]]
print(pairs)         # [(0, 1), (0, 2), (1, 2)]

Generator expressions

Swap [] for () and nothing is built up front — values are produced lazily, one at a time. Ideal when you only aggregate.

Python
total = sum(x * x for x in range(1_000_000))   # no million-item list in memory
print(total)                                    # 333332833333500000
print(any(n > 10 for n in [3, 12, 5]))          # True

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report