8 Functional programming with comprehensions
This chapter introduces Python’s take on functional programming as a way to write shorter, more reliable code by favoring small functions, immutable data, and treating functions as first-class values. While Python is not purely functional, it adopts many functional techniques—most notably comprehensions—to express data transformations succinctly. The chapter stresses deciding between comprehensions and traditional loops: use a comprehension when the goal is to build a new collection from an iterable, and use a loop when you care about side effects rather than a returned value.
Readers learn the core mechanics of list comprehensions—transforming and filtering—and how to extend them with conditions, nesting, and iteration over any iterable, including files. The text contrasts comprehensions with map and filter, encourages breaking multi-part comprehensions across lines for readability, and shows when generator expressions are preferable for memory efficiency. It then broadens the toolkit with set and dict comprehensions, demonstrating how expressions (not just functions) drive concise transformations, and how passing functions into other functions enables flexible, composable behavior.
Through a progression of small projects, the chapter grounds these ideas in practice: joining ranges of numbers into strings, summing only numeric tokens, flattening nested lists, translating file contents to Pig Latin with a nested generator expression, flipping key-value pairs in dictionaries, and applying transformations to dict values. It also uses set comprehensions to find “supervocalic” words and builds a gematria mapping with enumerate and ascii lowercase to score words and find score-matched dictionary entries. The overall outcome is a repeatable pattern for spotting transformation-and-filtering problems, choosing the right comprehension or generator, and balancing readability, performance, and expressiveness.
Graph of the family for nested list comprehensions
Summary
Comprehensions are, without a doubt, one of the most difficult topics for people to learn when they start using Python. The syntax is a bit weird, and it’s not even obvious where and when to use comprehensions. In this chapter, you saw many examples of how and when to use comprehensions, which will hopefully help you not only to use them, but also to see opportunities to do so.
FAQ
When should I use a comprehension instead of a traditional for loop?
Use a comprehension when your goal is to transform or filter an iterable into a new collection (list, set, or dict). Use a regular for loop when you care about side effects (e.g., writing files, printing) and don’t need a returned collection.What kinds of comprehensions does Python provide, and how do they differ from generator expressions?
- List comprehension: [expr for item in iterable if cond]- Set comprehension: {expr for item in iterable if cond}
- Dict comprehension: {key_expr: value_expr for item in iterable if cond}
- Generator expression: (expr for item in iterable if cond), which yields items lazily (one by one) instead of building a collection immediately.
What are the three parts of a comprehension?
Comprehensions have: (1) an expression that produces each output element, (2) an iteration clause (for ... in ...), and optionally (3) a filtering clause (if ...). Example: [x*x for x in range(10) if x % 2 == 0].How can I write readable comprehensions?
Break them across lines to separate expression, iteration, and condition. For example:[x*x
for x in range(5)
if x % 2]
Python Workout, Second Edition ebook for free