Bird
Raised Fist0
Pythonprogramming~20 mins

Nested lists 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 Lists Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested list indexing
What is the output of this Python code?
Python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = matrix[1][2]
print(result)
A6
B5
C4
DIndexError
Attempts:
2 left
๐Ÿ’ก Hint
Remember that indexing starts at 0 and matrix[1] is the second list.
โ“ Predict Output
intermediate
2:00remaining
Length of nested lists
What is the output of this code?
Python
nested = [[1, 2], [3, 4, 5], [6]]
length = len(nested[1])
print(length)
A1
B3
C2
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
len() returns the number of elements in the list at index 1.
โ“ Predict Output
advanced
2:00remaining
Output of nested list comprehension
What is the output of this code?
Python
matrix = [[i * j for j in range(3)] for i in range(3)]
print(matrix)
A[[0, 0, 0], [1, 1, 1], [2, 2, 2]]
B[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
C[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Each element is i multiplied by j for j in 0 to 2, repeated for i in 0 to 2.
โ“ Predict Output
advanced
2:00remaining
Value after nested list modification
What is the value of nested[0][1] after running this code?
Python
nested = [[1, 2], [3, 4]]
nested[0][1] = nested[1][0]
print(nested[0][1])
A2
BIndexError
C4
D3
Attempts:
2 left
๐Ÿ’ก Hint
nested[1][0] is assigned to nested[0][1].
๐Ÿง  Conceptual
expert
2:00remaining
Error type from invalid nested list access
What error does this code raise?
Python
lst = [[1, 2], [3, 4]]
print(lst[2][0])
AIndexError
BTypeError
CAttributeError
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Check if the outer list has an element at index 2.

Practice

(1/5)
1. What is a nested list in Python?
easy
A. A list with only one element
B. A list that contains other lists as its elements
C. A list that cannot be changed
D. A list that stores only numbers

Solution

  1. Step 1: Understand list elements

    A list can hold many types of elements, including other lists.
  2. Step 2: Define nested list

    A nested list is a list where some elements are themselves lists.
  3. Final Answer:

    A list that contains other lists as its elements -> Option B
  4. Quick Check:

    Nested list = list inside list [OK]
Hint: Think of a list inside another list [OK]
Common Mistakes:
  • Confusing nested list with single-level list
  • Thinking nested list is immutable
  • Assuming nested list holds only numbers
2. Which of the following is the correct syntax to create a nested list with two inner lists?
easy
A. [[1, 2], [3, 4]]
B. [1, 2, 3, 4]
C. [1, [2, 3], 4]
D. [[1, 2], 3, 4]

Solution

  1. Step 1: Identify nested list syntax

    A nested list has lists inside the main list, each enclosed in brackets.
  2. Step 2: Check options

    [[1, 2], [3, 4]] has two inner lists: [1, 2] and [3, 4], correctly nested.
  3. Final Answer:

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

    Two inner lists = double brackets [OK]
Hint: Look for double brackets for nested lists [OK]
Common Mistakes:
  • Using single brackets only
  • Mixing elements and lists incorrectly
  • Missing commas between inner lists
3. What is the output of the following code?
matrix = [[1, 2], [3, 4]]
print(matrix[1][0])
medium
A. 3
B. 1
C. 4
D. 2

Solution

  1. Step 1: Access outer list element

    matrix[1] accesses the second inner list: [3, 4].
  2. Step 2: Access inner list element

    matrix[1][0] accesses the first element of [3, 4], which is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    matrix[1][0] = 3 [OK]
Hint: First index outer list, second index inner list [OK]
Common Mistakes:
  • Mixing up indexes (using [0][1] instead)
  • Forgetting zero-based indexing
  • Confusing inner and outer list positions
4. Find the error in this code snippet:
data = [[1, 2], [3, 4]]
print(data[2][0])
medium
A. No error, output is 3
B. SyntaxError due to missing brackets
C. TypeError because data is not a list
D. IndexError because data[2] does not exist

Solution

  1. Step 1: Check list length

    data has two elements: indexes 0 and 1 only.
  2. Step 2: Accessing invalid index

    data[2] tries to access a third element which does not exist, causing IndexError.
  3. Final Answer:

    IndexError because data[2] does not exist -> Option D
  4. Quick Check:

    Index out of range = IndexError [OK]
Hint: Check list length before accessing index [OK]
Common Mistakes:
  • Assuming index 2 exists
  • Confusing syntax error with runtime error
  • Ignoring zero-based indexing
5. Given a nested list nums = [[1, 2, 3], [4, 5], [6]], which code correctly prints all elements in order?
hard
A. print(nums[0][0], nums[1][1], nums[2][0])
B. for i in range(len(nums)): print(nums[i])
C. for sublist in nums: for num in sublist: print(num)
D. for num in nums: print(num)

Solution

  1. Step 1: Understand nested iteration

    To print all elements, loop over each inner list, then each element inside it.
  2. Step 2: Analyze options

    for sublist in nums: for num in sublist: print(num) uses two loops: outer for sublist, inner for num, printing each number.
  3. Final Answer:

    for sublist in nums: for num in sublist: print(num) -> Option C
  4. Quick Check:

    Nested loops print all elements [OK]
Hint: Use nested loops to access nested list elements [OK]
Common Mistakes:
  • Printing inner lists instead of elements
  • Using single loop only
  • Hardcoding indexes without loops