Skip to content

Python Comprehensions: list, dict, set, and generator

  • How list comprehensions replace simple for loops 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.
  • Python 3.9+ installed
  • Familiarity with loops and functions

A list comprehension builds a new list by applying an expression to each element of an iterable, with an optional filter.

# Traditional loop
result = []
for x in range(10):
if x % 2 == 0:
result.append(x ** 2)
# List comprehension — same result, one line
result = [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 IDs
ids = [o["id"] for o in orders]
# ['ORD-001', 'ORD-002', 'ORD-003', 'ORD-004']
# Filter to shipped orders
shipped = [o for o in orders if o["status"] == "shipped"]
# Apply a transformation: add tax
with_tax = [
{**o, "total": round(o["amount"] * 1.08, 2)}
for o in orders
]
# Extract IDs of large pending orders
flagged = [
o["id"]
for o in orders
if o["status"] == "pending" and o["amount"] > 40
]
# ['ORD-002']

A dict comprehension builds a new dict from an iterable.

# Map order ID to amount
prices = {o["id"]: o["amount"] for o in orders}
# {'ORD-001': 149.99, 'ORD-002': 49.99, ...}
# Invert a dict
original = {"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 threshold
large_orders = {oid: amt for oid, amt in prices.items() if amt > 100}
# Normalize keys to lowercase
raw_config = {"API_KEY": "abc", "TIMEOUT": "30"}
config = {k.lower(): v for k, v in raw_config.items()}
# {'api_key': 'abc', 'timeout': '30'}

A set comprehension builds a set — unique values, no duplicates.

# Unique statuses
statuses = {o["status"] for o in orders}
# {'shipped', 'pending', 'cancelled'}
# Unique first characters of order IDs
prefixes = {o["id"][0] for o in orders}
# {'O'}
# Faster than: list(set(...))

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 memory
squares_list = [x ** 2 for x in range(1_000_000)]
# Generator expression: computes on demand, uses almost no memory
squares_gen = (x ** 2 for x in range(1_000_000))
# Both iterate the same way
for sq in squares_gen:
if sq > 100:
break
# Useful when passing to functions that consume iterables
total = sum(o["amount"] for o in orders) # no intermediate list
count = 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.

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 lists
batches = [["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 grid
grid = [(row, col) for row in range(3) for col in range(3)]
# [(0, 0), (0, 1), (0, 2), (1, 0), ...]

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 loop
result = []
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 orders
  • Using a comprehension for side effects. [print(x) for x in items] builds a list of Nones as a side effect of printing. Use a for loop 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.