6 min read
ā¢Question 17 of 41mediumList Comprehensions
Advanced list comprehension patterns.
What You'll Learn
- List, dict, and set comprehension syntax
- Nested comprehensions and flattening
- Conditional filtering and transformation
- When to use comprehensions vs loops
- Performance considerations
Understanding Comprehensions
Comprehensions provide a concise, readable way to create collections in Python. They're more Pythonic than equivalent for loops and often faster.
Types of comprehensions:
- List:
[expr for x in iter] - Dict:
{key: val for x in iter} - Set:
{expr for x in iter} - Generator:
(expr for x in iter)
List Comprehension Syntax
code.pyPython
# Basic syntax
# [expression for item in iterable]
squares = [x**2 for x in range(5)]
# [0, 1, 4, 9, 16]
# With condition (filter)
# [expression for item in iterable if condition]
evens = [x for x in range(10) if x % 2 == 0]
# [0, 2, 4, 6, 8]
# With if-else (transform)
# [expr_if_true if condition else expr_if_false for item in iterable]
labels = ["even" if x % 2 == 0 else "odd" for x in range(5)]
# ["even", "odd", "even", "odd", "even"]Nested Comprehensions
code.pyPython
# Flatten 2D list
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat = [num for row in matrix for num in row]
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Reading order: left to right (outer to inner)
# Equivalent loop:
# for row in matrix:
# for num in row:
# flat.append(num)
# Create 2D list (matrix)
grid = [[i * j for j in range(1, 4)] for i in range(1, 4)]
# [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
# Cartesian product
pairs = [(x, y) for x in range(3) for y in range(3)]
# [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)]Dictionary Comprehension
code.pyPython
# Basic dict comprehension
squares = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
# From two lists
keys = ['a', 'b', 'c']
values = [1, 2, 3]
d = {k: v for k, v in zip(keys, values)}
# {'a': 1, 'b': 2, 'c': 3}
# Swap keys and values
original = {'a': 1, 'b': 2, 'c': 3}
swapped = {v: k for k, v in original.items()}
# {1: 'a', 2: 'b', 3: 'c'}
# Filter dict
scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78}
passed = {name: score for name, score in scores.items() if score >= 80}
# {'Alice': 85, 'Bob': 92}
# Transform keys or values
upper_keys = {k.upper(): v for k, v in scores.items()}Set Comprehension
code.pyPython
# Unique values only
squares = {x**2 for x in range(-5, 6)}
# {0, 1, 4, 9, 16, 25}
# Extract unique first letters
words = ["apple", "banana", "apricot", "cherry"]
first_letters = {word[0] for word in words}
# {'a', 'b', 'c'}Generator Expression
Memory-efficient for large datasets:
code.pyPython
# Generator expression (lazy evaluation)
gen = (x**2 for x in range(1000000))
# Use with functions that consume iterables
total = sum(x**2 for x in range(1000000))
# Memory comparison
import sys
list_comp = [x for x in range(10000)]
gen_expr = (x for x in range(10000))
print(sys.getsizeof(list_comp)) # ~87,000 bytes
print(sys.getsizeof(gen_expr)) # ~112 bytesWhen to Use Comprehensions
code.pyPython
# GOOD: Simple transformations
squares = [x**2 for x in range(10)]
# GOOD: Filtering
adults = [p for p in people if p.age >= 18]
# BAD: Side effects (use regular loop)
# [print(x) for x in range(10)] # Don't do this!
# BAD: Complex logic (use regular loop)
# Hard to read nested comprehensions with multiple conditionsInterview Tip
When asked about comprehensions:
- Syntax:
[expr for x in iter if cond] - More readable and often faster than loops
- List, dict, set, and generator variations
- Don't overuse - complex logic is harder to read
- Generator expressions for memory efficiency