List Comprehension
A list comprehension is a concise syntax for creating a new list by applying an expression to each item in an iterable, optionally filtered by a condition....
What is a List Comprehension?
A list comprehension is a concise syntax for creating a new list by applying an expression to each item in an iterable, optionally filtered by a condition. The general form is: [expression for item in iterable] or [expression for item in iterable if condition]. Example: [x**2 for x in range(1, 6)] gives [1, 4, 9, 16, 25]. List comprehensions are more Pythonic, faster (internally optimised), and often more readable than equivalent for loops with append.
Filtering with Conditions
Adding an if clause filters which items contribute to the result: [x for x in range(20) if x % 3 == 0] gives [0, 3, 6, 9, 12, 15, 18]. The expression and condition can be different: [x**2 for x in range(10) if x % 2 == 0] squares only even numbers. Multiple conditions can be combined with and or or: [x for x in data if x > 0 and x < 100]. The condition is evaluated for every item; only items where it is True appear in the result.
Nested List Comprehensions
List comprehensions can contain multiple for clauses for nested iteration. [[row[i] for row in matrix] for i in range(4)] transposes a matrix. The equivalent nested loop: result = []; for i in range(4): col = []; for row in matrix: col.append(row[i]); result.append(col). The outer for clause in a nested comprehension corresponds to the outer loop. Deeply nested comprehensions reduce readability; for more than two levels, regular loops or helper functions are clearer.
Generator Expressions
Replace the square brackets with parentheses to get a generator expression instead of a list: (x**2 for x in range(10)). A generator expression is lazy — it computes values one at a time as they are requested, using minimal memory. It is ideal when you only need to iterate once: sum(x**2 for x in range(10)) sums the squares without building an intermediate list. When you need random access or multiple passes, use a list comprehension; when you just need to iterate or aggregate, prefer a generator.
Dict and Set Comprehensions
The same comprehension syntax works for dictionaries and sets. Dict comprehension: {k: v for k, v in pairs} or {x: x**2 for x in range(5)} gives {0:0, 1:1, 2:4, 3:9, 4:16}. Set comprehension: {x**2 for x in range(-3, 4)} gives {0, 1, 4, 9} — duplicates from squaring -2 and 2 are automatically removed. These comprehensions create their respective types directly and are idiomatic in modern Python code.
Exam Tip
List comprehension exams test three things. First, the difference between [expr for x in it if cond] (filters items) and [expr if cond else other for x in it] (transforms every item with a ternary). Second, nesting order: the leftmost for clause is the outer loop. Third, memory: a list comprehension creates the full list in memory; a generator expression is lazy. Common question: what does [x for x in range(5) if x % 2] return? [1, 3] — because 1 and 3 are truthy (odd), while 0, 2, 4 are falsy (even).