Python Comprehensions: list, dict, set, and generator
What you’ll learn
Section titled “What you’ll learn”- How list comprehensions replace simple
forloops with a single expression. - Dict and set comprehensions for building collections from existing data.
- Generator expressions for lazy evaluation.
- When comprehensions improve readability and when they hurt it.
Prerequisites
Section titled “Prerequisites”- Python 3.9+ installed
- Familiarity with loops and functions
List comprehensions
Section titled “List comprehensions”A list comprehension builds a new list by applying an expression to each element of an iterable, with an optional filter.
# Traditional loopresult = []for x in range(10): if x % 2 == 0: result.append(x ** 2)
# List comprehension — same result, one lineresult = [x ** 2 for x in range(10) if x % 2 == 0]print(result) # [0, 4, 16, 36, 64]The structure is [expression for variable in iterable if condition]. The if part is optional.
Practical examples with an OrdersPortal dataset:
orders = [ {"id": "ORD-001", "amount": 149.99, "status": "shipped"}, {"id": "ORD-002", "amount": 49.99, "status": "pending"}, {"id": "ORD-003", "amount": 299.00, "status": "shipped"}, {"id": "ORD-004", "amount": 19.99, "status": "cancelled"},]
# Extract all order IDsids = [o["id"] for o in orders]# ['ORD-001', 'ORD-002', 'ORD-003', 'ORD-004']
# Filter to shipped ordersshipped = [o for o in orders if o["status"] == "shipped"]
# Apply a transformation: add taxwith_tax = [ {**o, "total": round(o["amount"] * 1.08, 2)} for o in orders]
# Extract IDs of large pending ordersflagged = [ o["id"] for o in orders if o["status"] == "pending" and o["amount"] > 40]# ['ORD-002']Dict comprehensions
Section titled “Dict comprehensions”A dict comprehension builds a new dict from an iterable.
# Map order ID to amountprices = {o["id"]: o["amount"] for o in orders}# {'ORD-001': 149.99, 'ORD-002': 49.99, ...}
# Invert a dictoriginal = {"a": 1, "b": 2, "c": 3}inverted = {v: k for k, v in original.items()}# {1: 'a', 2: 'b', 3: 'c'}
# Filter: keep only entries above a thresholdlarge_orders = {oid: amt for oid, amt in prices.items() if amt > 100}
# Normalize keys to lowercaseraw_config = {"API_KEY": "abc", "TIMEOUT": "30"}config = {k.lower(): v for k, v in raw_config.items()}# {'api_key': 'abc', 'timeout': '30'}Set comprehensions
Section titled “Set comprehensions”A set comprehension builds a set — unique values, no duplicates.
# Unique statusesstatuses = {o["status"] for o in orders}# {'shipped', 'pending', 'cancelled'}
# Unique first characters of order IDsprefixes = {o["id"][0] for o in orders}# {'O'}
# Faster than: list(set(...))Generator expressions
Section titled “Generator expressions”A generator expression has the same syntax as a list comprehension but uses () instead of []. It is lazy — values are computed one at a time as you iterate, not all at once.
# List comprehension: builds the entire list in memorysquares_list = [x ** 2 for x in range(1_000_000)]
# Generator expression: computes on demand, uses almost no memorysquares_gen = (x ** 2 for x in range(1_000_000))
# Both iterate the same wayfor sq in squares_gen: if sq > 100: break
# Useful when passing to functions that consume iterablestotal = sum(o["amount"] for o in orders) # no intermediate listcount = sum(1 for o in orders if o["status"] == "shipped")max_amount = max(o["amount"] for o in orders)any_pending = any(o["status"] == "pending" for o in orders)Use generator expressions when you only need to iterate once and do not need random access.
Nested comprehensions
Section titled “Nested comprehensions”You can nest comprehensions for flat iteration over nested structures, but limit to one level of nesting — deeper than that, use explicit loops.
# Flatten a list of listsbatches = [["ORD-001", "ORD-002"], ["ORD-003"], ["ORD-004", "ORD-005"]]all_ids = [oid for batch in batches for oid in batch]# ['ORD-001', 'ORD-002', 'ORD-003', 'ORD-004', 'ORD-005']
# 2D gridgrid = [(row, col) for row in range(3) for col in range(3)]# [(0, 0), (0, 1), (0, 2), (1, 0), ...]When to use and when not to
Section titled “When to use and when not to”Use a comprehension when:
- The logic fits naturally in one expression.
- The purpose is immediately clear from reading it.
- You are building a new collection from an existing one.
Use a regular loop when:
- The body has multiple statements or complex branching.
- You need to handle exceptions per item.
- Readability suffers from the single-line constraint.
# Too complex for a comprehension — use a loopresult = []for order in orders: try: discounted = order["amount"] * 0.9 if discounted > 50: result.append({"id": order["id"], "discounted": discounted}) except KeyError: pass # skip malformed ordersCommon mistakes
Section titled “Common mistakes”- Using a comprehension for side effects.
[print(x) for x in items]builds a list ofNones as a side effect of printing. Use aforloop for side effects. - Modifying the source list inside a comprehension. Mutating the iterable you are iterating creates unpredictable results.
- Nesting more than two levels.
[x for a in b for x in a for ...]is unreadable. Use loops and intermediate variables instead.
Official references
Section titled “Official references”- Python docs: List Comprehensions
- Python docs: Generator Expressions
- PEP 202 — list comprehensions
- PEP 274 — dict comprehensions