Chapter 14 of 21
OOP: Inheritance & Polymorphism
Current Study Module
List Comprehensions & Generator Expressions
Decorators, Iterators & Generators
Chapter 13 • Expressive Data Transformations

Comprehensions & Generator Expressions

Comprehensions provide a concise, declarative syntax for transforming, filtering, and mapping data collections. Learn how CPython executes comprehensions faster than standard for loops via dedicated bytecode instructions, how to construct dictionary and set comprehensions, and why generator expressions save gigabytes of RAM.

1. List Comprehension Syntax & Performance

A list comprehension encapsulates mapping and filtering into a single expressive expression:[expression for item in iterable if condition]:

# Traditional imperative loop (Slower due to repeated list.append attribute lookups):
# squares = []
# for x in range(10):
#     if x % 2 == 0:
#         squares.append(x ** 2)

# Declarative Pythonic Comprehension (Faster via LIST_APPEND C-level bytecode):
squares = [x ** 2 for x in range(10) if x % 2 == 0]
print("Even squares:", squares)  # [0, 4, 16, 36, 64]

# Data sanitization example
raw_tags = ["  python ", "AI", " machine-learning ", "   FASTAPI  "]
clean_tags = [t.strip().lower() for t in raw_tags]
print("Clean tags:", clean_tags)

2. Dictionary & Set Comprehensions

You can apply the exact same declarative syntax to construct hash tables (dictionaries) and unique sets:

# 1. Dictionary Comprehension: {key_expr: value_expr for item in iterable}
users = [("u_101", "Alice", "Admin"), ("u_102", "Bob", "Viewer"), ("u_103", "Charlie", "Editor")]

# Fast O(1) lookup table keyed by user_id
user_directory = {uid: {"name": name, "role": role} for uid, name, role in users}
print("User 102 Role:", user_directory["u_102"]["role"])

# Swap keys and values in a dictionary
status_codes = {200: "OK", 404: "NOT_FOUND", 500: "SERVER_ERROR"}
inverted_codes = {name: code for code, name in status_codes.items()}
print("Inverted:", inverted_codes["NOT_FOUND"])  # 404

# 2. Set Comprehension: {expr for item in iterable}
text = "the quick brown fox jumps over the lazy dog"
unique_word_lengths = {len(word) for word in text.split()}
print("Unique word lengths:", unique_word_lengths)

3. Generator Expressions: The Secret to Constant Memory (O(1) RAM)

A list comprehension (using square brackets []) allocates and populates the entire list in memory immediately. A generator expression (using parentheses ()) yields items lazily on demand, consuming virtually zero memory:

import sys

# List Comprehension: Allocates 10 million integers in RAM immediately (~80 Megabytes!)
list_comp = [x * 2 for x in range(10_000_000)]
print(f"List Comprehension Memory: {sys.getsizeof(list_comp):,} bytes")  # ~89,095,160 bytes!

# Generator Expression: Evaluates on demand using a generator object (~200 Bytes!)
gen_expr = (x * 2 for x in range(10_000_000))
print(f"Generator Expression Memory: {sys.getsizeof(gen_expr):,} bytes")  # Only 208 bytes!

# Consume lazily
print("First item from generator:", next(gen_expr))  # 0
print("Second item from generator:", next(gen_expr)) # 2

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools