A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

List Comprehensions

College Depth 79 in the knowledge graph I know this Set as goal
528topics build on this
337prerequisites beneath it
See this on the map →
For LoopsList Operations and Methods+2 moreAlgorithm Design Basics
list comprehensions concise iteration functional style filter map

Core Idea

A list comprehension creates a new list by applying an expression to each element of an iterable, optionally filtered by a condition, in a single readable line: [f(x) for x in iterable if condition]. They are equivalent to a for loop with an accumulator list but are more concise and often faster. List comprehensions express the transformation declaratively (what the result should be) rather than imperatively (how to build it step by step). They are a widely used Python idiom and appear in similar forms in many modern languages.

How It's Best Learned

Rewrite existing for-loop accumulation patterns as list comprehensions. Start with simple expressions, then add filtering conditions. Verify output matches the loop version exactly.

Common Misconceptions

Explainer

You are comfortable with for loops and list operations — you know how to iterate through a collection and build up a new list by appending to it inside a loop. List comprehensions are a more concise syntax for exactly that pattern, and once you see the correspondence, they become a natural part of how you write Python.

Consider a common pattern: starting with an empty list, looping through some items, transforming each one, and appending the result. For example, to square every number in a list: `squares = []`, then `for x in numbers: squares.append(x 2)`. The list comprehension equivalent is `squares = [x 2 for x in numbers]`. The expression before `for` (here `x ** 2`) is the transformation applied to each element. The `for x in numbers` part is the iteration. The result is a new list containing every transformed value. The logic is identical to the loop — the comprehension is just a more compact way to express it.

You can also add a filter with an `if` clause. To get only the even squares: `even_squares = [x 2 for x in numbers if x % 2 == 0]`. This is equivalent to putting an if-statement inside the loop before the append. The reading order is: "give me `x 2` for each `x` in `numbers`, but only if `x` is even." You can also nest comprehensions for working with multi-dimensional data — `[cell for row in matrix for cell in row]` flattens a list of lists — though nested comprehensions should be used sparingly because they become hard to read quickly.

The advantage of comprehensions is not just brevity — they also signal intent. When a reader sees a list comprehension, they immediately know the result is a new list derived from an existing iterable. A for loop could be doing anything: printing, modifying global state, calling APIs. A comprehension declares "I am building a list by transforming data," which makes the code's purpose clear at a glance. As a rule of thumb, if the transformation and filter fit comfortably on one line and are easy to read, use a comprehension. If the logic requires multiple statements, intermediate variables, or complex branching, stick with a regular loop. The goal is always readability — comprehensions are a tool for clarity, not a puzzle to see how much logic you can compress into a single line.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsWhile LoopsFor LoopsArrays and ListsArray Indexing and BoundsIterating Over CollectionsNested Loops and Multi-Level IterationList Comprehensions

Longest path: 80 steps · 337 total prerequisite topics

Prerequisites (4)

Leads To (1)