Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a nested list comprehension in Python?
A nested list comprehension is a way to create a list inside another list comprehension, allowing you to build multi-dimensional lists or perform operations on nested loops in a single, concise expression.
Click to reveal answer
beginner
How does the order of 'for' loops affect a nested list comprehension?
The order of 'for' loops in a nested list comprehension matches the order of nested loops in regular code. The first 'for' is the outer loop, and the second 'for' is the inner loop, which runs completely for each iteration of the outer loop.
Click to reveal answer
beginner
Example: What does this nested list comprehension do? [[x * y for y in range(3)] for x in range(2)]
It creates a 2D list where each inner list contains the products of x and y. For x=0 and x=1, it multiplies by y=0,1,2, resulting in [[0, 0, 0], [0, 1, 2]].
Click to reveal answer
intermediate
Can nested list comprehensions replace nested for loops?
Yes, nested list comprehensions can replace nested for loops to create lists in a shorter and more readable way, especially when building or transforming multi-dimensional lists.
Click to reveal answer
intermediate
What is a common use case for nested list comprehensions?
They are commonly used to create matrices, flatten nested lists, or apply operations across multiple dimensions in a compact and readable way.
Click to reveal answer
What does the outer 'for' loop represent in a nested list comprehension?
AThe first loop that runs once per outer element
BThe last loop that runs for each inner element
CA conditional filter
DA function call
✗ Incorrect
The outer 'for' loop runs once per outer element, controlling how many inner lists are created.
What is the output of [[i + j for j in range(2)] for i in range(3)]?
A[[0, 1, 2], [0, 1, 2]]
B[[0, 1], [1, 2], [2, 3]]
C[[0, 1], [0, 1], [0, 1]]
D[[1, 2], [2, 3], [3, 4]]
✗ Incorrect
For i=0, j=0,1 → [0,1]; i=1 → [1,2]; i=2 → [2,3].
Which of these is a valid nested list comprehension?
A[x*y for x in range(2) y in range(3)]
B[x for y in range(3) for x in range(2)]
C[x*y for x in range(2), y in range(3)]
D[[x*y for y in range(3)] for x in range(2)]
✗ Incorrect
Option D correctly nests two 'for' loops inside the list comprehension.
How can nested list comprehensions improve code?
ABy removing the need for variables
BBy making code run slower
CBy making nested loops shorter and more readable
DBy avoiding all loops
✗ Incorrect
Nested list comprehensions make nested loops shorter and often easier to read.
What does this nested list comprehension do? [[i for i in range(3)] for _ in range(2)]
ACreates two lists each with [0, 1, 2]
BCreates one list with [0, 1, 2, 0, 1, 2]
CCreates a list of numbers from 0 to 5
DCreates an empty list
✗ Incorrect
It creates two inner lists, each containing [0, 1, 2].
Explain how nested list comprehensions work and how they relate to nested for loops.
Think about how loops inside loops work and how list comprehensions can replace them.
You got /5 concepts.
Describe a real-life example where you might use a nested list comprehension.
Imagine working with tables or grids of numbers.
You got /5 concepts.
Practice
(1/5)
1. What does the following nested list comprehension do? [[x * 2 for x in range(3)] for _ in range(2)]
easy
A. Creates a list of two lists, each with numbers from 0 to 3
B. Creates a flat list of numbers doubled from 0 to 5
C. Creates a list of three lists, each with numbers doubled from 0 to 1
D. Creates a list of two lists, each with numbers doubled from 0 to 2
Solution
Step 1: Understand inner list comprehension
The inner part [x * 2 for x in range(3)] creates a list by doubling numbers 0, 1, 2 resulting in [0, 2, 4].
Step 2: Understand outer list comprehension
The outer loop runs twice (for _ in range(2)), repeating the inner list twice, making [[0, 2, 4], [0, 2, 4]].
Final Answer:
Creates a list of two lists, each with numbers doubled from 0 to 2 -> Option D
Quick Check:
Nested loops create repeated doubled lists [OK]
Hint: Inner loop builds list, outer repeats it [OK]
Common Mistakes:
Confusing outer and inner loops
Thinking it creates a flat list
Miscounting range values
2. Which of the following is the correct syntax for a nested list comprehension that creates a 3x3 matrix of zeros?
[[0 for _ in range(3)] for _ in range(3)] uses two for loops inside list brackets correctly: outer and inner loops create rows and columns.
Step 2: Check other options for syntax errors
[0 for _ in range(3) for _ in range(3)] is flat comprehension, not nested. [[0] * 3] * 3 uses list multiplication but not comprehension. [0] * 3 for _ in range(3) is invalid syntax.
Final Answer:
[[0 for _ in range(3)] for _ in range(3)] -> Option C