Challenge - 5 Problems
Nested Lists Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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)
Attempts:
2 left
๐ก Hint
Remember that indexing starts at 0 and matrix[1] is the second list.
โ Incorrect
matrix[1] accesses the second list [4, 5, 6]. Then [2] accesses the third element in that list, which is 6.
โ Predict Output
intermediate2: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)
Attempts:
2 left
๐ก Hint
len() returns the number of elements in the list at index 1.
โ Incorrect
nested[1] is [3, 4, 5], which has 3 elements.
โ Predict Output
advanced2: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)
Attempts:
2 left
๐ก Hint
Each element is i multiplied by j for j in 0 to 2, repeated for i in 0 to 2.
โ Incorrect
For i=0: [0*0, 0*1, 0*2] = [0,0,0]; i=1: [0,1,2]; i=2: [0,2,4].
โ Predict Output
advanced2: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])
Attempts:
2 left
๐ก Hint
nested[1][0] is assigned to nested[0][1].
โ Incorrect
nested[1][0] is 3, so nested[0][1] becomes 3.
๐ง Conceptual
expert2:00remaining
Error type from invalid nested list access
What error does this code raise?
Python
lst = [[1, 2], [3, 4]] print(lst[2][0])
Attempts:
2 left
๐ก Hint
Check if the outer list has an element at index 2.
โ Incorrect
lst has only indices 0 and 1. Accessing lst[2] causes IndexError.