100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

List Comprehension in Python

A concise Python syntax for building lists from iterables in a single expression, including conditionals and the memory-difference vs generator expressions.

Data StructuresIntermediate12 min readJul 7, 2026
Analogies

1. Introduction

List comprehension is a concise, expressive Python syntax for creating a new list by applying an expression to each item of an iterable, optionally filtering items with a condition. It replaces the more verbose pattern of building a list with a for loop and repeated .append() calls.

🏏

Cricket analogy: '[run*4 if boundary else run for run in deliveries]' replaces a scorer manually looping ball by ball and appending totals to a tally sheet -- one concise line building the whole innings list.

Beyond brevity, list comprehensions are often more readable and can be slightly faster than equivalent explicit loops, because the looping and appending happen at the C level inside the Python interpreter.

🏏

Cricket analogy: A comprehension building an over's boundary count runs the looping internally at C speed, like a stadium's electronic scoreboard updating instantly rather than a scorer manually chalking each run by hand.

2. Syntax

python
# Basic syntax: [expression for item in iterable]
squares = [x ** 2 for x in range(6)]

# With a condition (filter)
evens = [x for x in range(10) if x % 2 == 0]

# With an if-else expression
labels = ["even" if x % 2 == 0 else "odd" for x in range(5)]

# Nested comprehension (flattening a 2D list)
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]

# Equivalent generator expression (uses parentheses)
squares_gen = (x ** 2 for x in range(6))

3. Explanation

A list comprehension has the general form [expression for item in iterable if condition], where the if condition clause is optional and filters which items are included. Multiple for clauses can be chained to flatten nested structures or produce combinations.

🏏

Cricket analogy: '[player for team in tournament for player in team.squad]' flattens every squad across a tournament into one list, like a stats site compiling every player across all IPL franchises into a single leaderboard.

A closely related construct is the generator expression, which uses parentheses () instead of square brackets []. While it looks almost identical, it produces items lazily one at a time instead of building the entire list in memory upfront.

🏏

Cricket analogy: '(run for run in deliveries)' is like a radio commentator calling out each run live as it happens rather than a printed scorecard prepared all at once -- lazy, one delivery at a time.

List comprehension vs generator expression memory difference: [x**2 for x in range(10**8)] builds the ENTIRE list in memory immediately (can be gigabytes), while (x**2 for x in range(10**8)) creates a generator that computes each value lazily on demand, using constant memory regardless of size.

Overusing deeply nested list comprehensions (multiple for and if clauses in one line) hurts readability. If a comprehension needs more than two levels of nesting or complex logic, a regular for loop is usually clearer and easier to debug.

4. Example

python
numbers = range(10)

squares = [x ** 2 for x in numbers]
print("squares:", squares)

evens_squared = [x ** 2 for x in numbers if x % 2 == 0]
print("evens squared:", evens_squared)

labels = ["even" if x % 2 == 0 else "odd" for x in range(5)]
print("labels:", labels)

matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
print("flat:", flat)

gen = (x ** 2 for x in range(5))
print("generator type:", type(gen))
print("generator values:", list(gen))

5. Output

text
squares: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
evens squared: [0, 4, 16, 36, 64]
labels: ['even', 'odd', 'even', 'odd', 'even']
flat: [1, 2, 3, 4, 5, 6]
generator type: <class 'generator'>
generator values: [0, 1, 4, 9, 16]

6. Key Takeaways

  • List comprehension syntax: [expression for item in iterable if condition].
  • The if clause filters items; an if-else before the for acts as a conditional expression per item.
  • Multiple for clauses can flatten nested lists in a single comprehension.
  • List comprehensions build the full list in memory immediately; generator expressions () compute values lazily, saving memory for large sequences.
  • List comprehensions are often faster and more readable than an equivalent for loop with .append().
  • Avoid overly complex, deeply nested comprehensions — prefer a regular loop when readability suffers.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#ListComprehensionInPython#List#Comprehension#Syntax#Explanation#DataStructures#StudyNotes#SkillVeris