Bird
Raised Fist0
Pythonprogramming~20 mins

Nested list comprehension in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Nested List Comprehension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested list comprehension with condition
What is the output of the following Python code?
Python
result = [[x * y for y in range(3)] for x in range(3)]
print(result)
A[[0, 0, 0], [0, 1, 2], [0, 1, 2]]
B[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
C[[0, 0, 0], [1, 2, 3], [2, 4, 6]]
D[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
Attempts:
2 left
๐Ÿ’ก Hint
Think about how the outer and inner loops multiply the indices.
โ“ Predict Output
intermediate
2:00remaining
Nested list comprehension with filtering
What is the output of this code snippet?
Python
result = [[x + y for y in range(4) if (x + y) % 2 == 0] for x in range(3)]
print(result)
A[[0, 2], [2, 4], [2, 4]]
B[[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
C[[0, 2], [0, 2], [0, 2]]
D[[0, 2], [1, 3], [2, 4]]
Attempts:
2 left
๐Ÿ’ก Hint
Only include sums that are even numbers.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in nested list comprehension
What error does this code raise when run?
Python
result = [[x * y for x in range(3)] for y in range(3)]
print(result[3][0])
ATypeError
BIndexError
CSyntaxError
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Check the indices used to access the list elements.
๐Ÿ“ Syntax
advanced
2:00remaining
Which nested list comprehension is syntactically correct?
Choose the option that is a valid nested list comprehension in Python.
A[x + y for x in range(3), y in range(3)]
B[[x + y for x in range(3) if y] for y in range(3)]
C[[x + y for x in range(3)] for y in range(3)]
D[[x + y for x in range(3) for y in range(3)]]
Attempts:
2 left
๐Ÿ’ก Hint
Remember the syntax for nested list comprehensions uses separate for clauses inside brackets.
๐Ÿš€ Application
expert
3:00remaining
Create a multiplication table using nested list comprehension
Which option creates a 5x5 multiplication table (list of lists) where each element is the product of its row and column indices (starting from 1)?
A[[i * j for j in range(1, 6)] for i in range(1, 6)]
B[[i * j for i in range(1, 6)] for j in range(1, 6)]
C[[i + j for j in range(1, 6)] for i in range(1, 6)]
D[[i * j for j in range(5)] for i in range(5)]
Attempts:
2 left
๐Ÿ’ก Hint
Rows and columns start at 1, so use range(1, 6).

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

  1. 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].
  2. 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]].
  3. Final Answer:

    Creates a list of two lists, each with numbers doubled from 0 to 2 -> Option D
  4. 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?
easy
A. [[0] * 3] * 3
B. [0 for _ in range(3) for _ in range(3)]
C. [[0 for _ in range(3)] for _ in range(3)]
D. [0] * 3 for _ in range(3)

Solution

  1. Step 1: Identify correct nested comprehension syntax

    [[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.
  2. 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.
  3. Final Answer:

    [[0 for _ in range(3)] for _ in range(3)] -> Option C
  4. Quick Check:

    Nested loops inside brackets = correct syntax [OK]
Hint: Nested loops inside brackets create nested lists [OK]
Common Mistakes:
  • Using flat comprehension instead of nested
  • Incorrect list multiplication for nested lists
  • Syntax errors with missing brackets
3. What is the output of this code?
matrix = [[i + j for j in range(3)] for i in range(2)]
print(matrix)
medium
A. [[0, 1, 2], [1, 2, 3]]
B. [[0, 1, 2], [0, 1, 2]]
C. [[0, 1], [1, 2], [2, 3]]
D. [[0, 1, 2, 3], [1, 2, 3, 4]]

Solution

  1. Step 1: Understand inner loop values

    For each i, inner loop adds j (0,1,2) to i. For i=0: [0+0, 0+1, 0+2] = [0,1,2]. For i=1: [1+0, 1+1, 1+2] = [1,2,3].
  2. Step 2: Combine results into outer list

    The outer loop runs for i in range(2), so final list is [[0,1,2], [1,2,3]].
  3. Final Answer:

    [[0, 1, 2], [1, 2, 3]] -> Option A
  4. Quick Check:

    Sum of i and j in nested loops = output [OK]
Hint: Add outer and inner loop vars for each element [OK]
Common Mistakes:
  • Mixing i and j values
  • Confusing list dimensions
  • Incorrect range usage
4. Find the error in this nested list comprehension:
result = [[x * y for x in range(3)] for y in range(3)]
print(result)
medium
A. No error, code runs correctly
B. Inner loop variable x is undefined
C. Outer loop variable y is used before definition
D. Syntax error due to missing brackets

Solution

  1. Step 1: Check variable definitions

    Both x and y are defined in their respective loops: x in inner, y in outer.
  2. Step 2: Verify syntax and logic

    Syntax is correct with proper brackets. The code multiplies x and y for each pair in ranges 0 to 2.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Variables defined in loops, syntax correct [OK]
Hint: Check variable scopes in nested loops [OK]
Common Mistakes:
  • Thinking variables are undefined
  • Confusing loop order
  • Assuming syntax error without checking
5. Using nested list comprehension, how can you transform this nested list:
data = [[1, 0, 3], [0, 5, 0], [7, 0, 9]]

to replace all zeros with the string 'zero'?
hard
A. [['zero' if row == 0 else x for x in row] for row in data]
B. [['zero' if x == 0 else x for x in row] for row in data]
C. [['zero' if x == 0 else x for row in data] for x in row]
D. [['zero' if x == 0 else x for x in data] for row in data]

Solution

  1. Step 1: Use nested loops to access each element

    Outer loop goes through each row, inner loop through each element x in that row.
  2. Step 2: Replace zeros with 'zero' using conditional expression

    Use 'zero' if x == 0 else x inside inner loop to transform elements.
  3. Step 3: Combine into nested list comprehension

    Putting it together: [[ 'zero' if x == 0 else x for x in row ] for row in data].
  4. Final Answer:

    [['zero' if x == 0 else x for x in row] for row in data] -> Option B
  5. Quick Check:

    Conditional inside nested comprehension replaces zeros [OK]
Hint: Use if-else inside inner loop for element replacement [OK]
Common Mistakes:
  • Mixing loop variables
  • Placing condition on wrong variable
  • Incorrect loop order or syntax