Comprehensions & generators
List/dict/set comprehensions, generator expressions, and lazy iteration.
Build a lazy generator to read file lines in Python
Create a generator function that yields file lines one at a time, avoiding loading the entire file into memory, and demonstrate its lazy processing.
def lazy_lines(filepath):
"""Yield lines from a file one at a time without loading the whole file into memory."""
with open(filepath, 'r', encoding='utf-8') as file:
for line in file:
yield line.rstrip('\n')
if __name__ == "__main__":
# Create a sample file to demonstrate
sample_c…
How to Lazily Transform Items in Python with a Generator
Map a transform function over an iterable lazily with a generator so items are processed on demand, not up front.
def lazy_map(items, transform):
for item in items:
yield transform(item)
def double(x):
return x * 2
def upper(s):
return s.upper()
if __name__ == "__main__":
numbers = [1, 2, 3, 4, 5]
doubled = lazy_map(numbers, double)
print("Doubled numbers:", end=" ")
for value in doubled:
…
How to Parse CSV Rows as Generator Dicts in Python
Reads a CSV file and yields each row as a dictionary one at a time using a generator, so the file is processed lazily.
import csv
from pathlib import Path
def csv_to_dicts(filepath):
with open(filepath, mode="r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
yield row
if __name__ == "__main__":
sample_csv = Path("sample_data.csv")
sample_csv.write_text…
Memory efficient map over large file in Python
A generator-based streaming map that processes a large file line by line without loading the whole file into memory.
import sys
def process_lines(file_path):
"""Memory-efficient map over a large file: yields processed lines."""
with open(file_path, 'r') as f:
for line in f:
# Example mapping: strip whitespace and uppercase
yield line.strip().upper()
if __name__ == "__main__":
# Use a sma…
Browse by section
Each section groups closely related Python snippets.
Comprehensions & generators — Python code examples
What you will find here
This page collects comprehensions & generators snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.