List Comprehensions#
Note
Source: Python-specific — no equivalent exists in the C# edition.
List comprehensions are a concise Python idiom for building lists from
sequences. Every list comprehension has an equivalent for loop; the
examples below show both forms so you can see the correspondence.
A common pattern with for loops is building a new list by transforming
or filtering an existing sequence:
squares = []
for n in range(1, 6):
squares.append(n ** 2)
print(squares)
Output:
[1, 4, 9, 16, 25]
Python provides a compact notation for exactly this pattern called a list comprehension:
[expression for item in iterable]
The result is a new list containing expression evaluated once for each
item in iterable.
Basic Form#
The squares example above becomes:
squares = [n ** 2 for n in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
More examples:
# ASCII codes of each character in a string
codes = [ord(ch) for ch in "hello"]
print(codes)
Output:
[104, 101, 108, 108, 111]
# Lengths of each word in a list
words = ["cat", "elephant", "ox"]
lengths = [len(w) for w in words]
print(lengths)
Output:
[3, 8, 2]
Filtered Form#
Add an if clause to keep only items that satisfy a condition:
[expression for item in iterable if condition]
The equivalent for loop is:
result = []
for item in iterable:
if condition:
result.append(expression)
Example — keep only the even numbers from 0 to 19:
evens = [n for n in range(20) if n % 2 == 0]
print(evens)
Output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Example — extract only the vowels from a string:
s = "introduction"
vowels = [ch for ch in s if ch in "aeiou"]
print(vowels)
Output:
['i', 't', 'o', 'd', 'u', 't', 'i', 'o']
Nested Comprehensions#
You can nest for clauses to iterate over two sequences simultaneously.
This produces a flat list — not a list of lists:
pairs = [(r, c) for r in range(3) for c in range(3)]
print(pairs)
Output:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
The leftmost for is the outer loop; the rightmost is the inner loop —
the same order as nested for statements written out explicitly.
When to Use List Comprehensions#
Use a list comprehension when the goal is to produce a list from a
sequence. Prefer a regular for loop when the body has side effects
(printing, writing a file, mutating external state) or when the logic is
complex enough that a one-liner hurts readability.
A good rule of thumb: if you can read the comprehension aloud as a single English phrase — “the square of n for n in range 1 to 5” — it is probably clear enough to use.