Overview

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
7 1

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]
Why does ','.join(range(15)) fail, and how do I fix it?str.join expects strings, not integers. Convert each number first, ideally with a generator expression: ','.join(str(n) for n in range(15)).
When should I prefer a generator expression over a list comprehension?Prefer a generator expression when the consumer (e.g., sum, any, all, max) can iterate directly and you want to save memory. Example: sum(x*x for x in range(100000)) avoids building the whole list in memory.
How do I flatten a list of lists with a comprehension?Use a nested list comprehension that iterates the outer list and then each inner list: [elem for sublist in data for elem in sublist].
How can I transform every word in a file (e.g., Pig Latin) efficiently?Iterate over lines, then words, and use a generator expression with join to avoid materializing a large list: ' '.join(transform(word) for line in open(filename) for word in line.split()).
What’s a set comprehension, and how can I use it to find “supervocalic” words?A set comprehension builds a set: {expr for item in iterable if cond}. To find words containing all vowels, use subset testing with sets: vowels = {'a','e','i','o','u'}; {w.strip() for w in open(path) if vowels < set(w.lower())}.
Is str.isdigit a reliable way to find integers in text?isdigit only returns True for non-empty strings of digits and rejects negatives like '-3'. For robust parsing, prefer try/except with int(s). Note that exception handling can’t be written inline inside a comprehension; wrap it in a helper function if needed.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Python Workout, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Python Workout, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Python Workout, Second Edition ebook for free