Write Data Helpers with Comprehensions and Generators in Python
Demonstrates list, dict, and set comprehensions plus generator expressions and generator functions for building concise data helpers.
Python code
30 lines# Basic comprehensions and generators demo
# List comprehension: squares of evens
squares = [x * x for x in range(10) if x % 2 == 0]
print("List comp:", squares)
# Dictionary comprehension: char -> count
text = "hello"
char_counts = {c: text.count(c) for c in set(text)}
print("Dict comp:", char_counts)
# Set comprehension: unique squares
unique_squares = {x * x for x in range(5)}
print("Set comp:", unique_squares)
# Generator: lazy evaluation, sum without storing list
sum_of_squares = sum(x * x for x in range(1, 101))
print("Generator sum:", sum_of_squares)
# Generator expression with condition
gen = (x * 2 for x in range(5) if x > 1)
print("Generator values:", list(gen))
# Generator function with yield
def countdown(n):
while n > 0:
yield n
n -= 1
print("Countdown:", list(countdown(3)))
Output
List comp: [0, 4, 16, 36, 64]
Dict comp: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
Set comp: {0, 1, 4, 9, 16}
Generator sum: 338350
Generator values: [4, 6, 8]
Countdown: [3, 2, 1]
How it works
Comprehensions provide a compact syntax to build lists, dicts, and sets by iterating over an iterable and optionally filtering with an if clause. Generator expressions use parentheses and produce values lazily, meaning they generate items on demand without storing the entire sequence in memory, which is efficient for large ranges. Generator functions use the yield keyword to produce a series of values while preserving state between calls, allowing complex logic without allocating a full list. Both comprehensions and generators are powerful tools for writing readable, Pythonic data helpers that transform or filter data efficiently.
Common mistakes
- Forgetting that list comprehensions produce lists eagerly, which can use excessive memory for large inputs.
- Using round parentheses for a generator expression but accidentally creating a tuple if not consumed.
- Overusing comprehensions for complex logic that obscures readability; consider a regular loop instead.
Variations
- Nested comprehensions to handle nested loops, e.g., [x*y for x in range(3) for y in range(3)].
- Using itertools.chain or map/filter as functional alternatives for simple transformations.
Real-world use cases
- Building derived lists like user IDs from a list of user objects in a web app response.
- Aggregating fast counts (e.g., word frequency) with dictionary comprehensions in log parsers.
- Streaming large file data with generator functions for memory-efficient processing.
Sponsored
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a Generator Pipeline in Python: Filter Then Map medium
- Build a lazy generator to read file lines in Python easy
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
Keep learning
Related tutorials and quizzes for this topic.