Community Python Snippet

When I Stop Reaching for List Comprehensions

I love comprehensions, but I have learned the three cases where they cost more than they save: nested filtering, side effects, and big intermediate lists. Here is the pattern I switch to in each.

When I Stop Reaching for List Comprehensions

I love comprehensions, but I have learned the three cases where they cost more than they save: nested filtering, side effects, and big intermediate lists. Here is the pattern I switch to in each.

Python
Compiler
3 snippets
py-list-comprehensions
py-generators
performance
ayomidegray

By @ayomidegray

February 11, 2026

·

Updated May 18, 2026

628 views

4

3.9 (9)

The comprehension is not the problem; the predicate is. The moment a filter has more than one boolean operator I extract it into a named function whose name reads like business logic (can_access_beta). The win is double: the comprehension reverts to one obvious line (pluck users where can_access_beta), and the rule itself is now unit-testable in isolation. I have caught real bugs in this exact extraction step because seeing or ('editor' in r['roles'] and 'beta' in r['features']) written as a function exposes the precedence the inline version is hiding.