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 more→→Algorithm 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 Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → While Loops → For Loops → Arrays and Lists → Array Indexing and Bounds → Iterating Over Collections → Nested Loops and Multi-Level Iteration → List Comprehensions

Longest path: 80 steps · 337 total prerequisite topics

Prerequisites (4)

Leads To (1)