Challenge - 5 Problems
2D Array Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of 2D array element access
What is the output of the following code snippet?
NumPy
import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) result = arr[1, 2] print(result)
Attempts:
2 left
💡 Hint
Remember that indexing starts at 0 and the first index is the row, second is the column.
✗ Incorrect
The element at row 1, column 2 is 60 because rows and columns start at 0. So arr[1, 2] accesses the second row and third column.
❓ data_output
intermediate2:00remaining
Extracting a row from a 2D array
What is the output of this code that extracts the second row from the array?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) row = arr[1, :] print(row)
Attempts:
2 left
💡 Hint
The colon (:) means all columns in the selected row.
✗ Incorrect
arr[1, :] selects all columns in the second row (index 1), which is [4, 5, 6].
❓ Predict Output
advanced2:00remaining
Selecting multiple elements with 2D indexing
What is the output of this code that selects elements from specific rows and columns?
NumPy
import numpy as np arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) result = arr[[0, 2], [1, 2]] print(result)
Attempts:
2 left
💡 Hint
When using two lists for indexing, elements are selected pairwise from each list.
✗ Incorrect
arr[[0, 2], [1, 2]] selects elements at (0,1) and (2,2), which are 10 and 45.
🔧 Debug
advanced2:00remaining
Identify the error in 2D array indexing
What error does this code raise when trying to access an element?
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) value = arr[2, 1] print(value)
Attempts:
2 left
💡 Hint
Check the size of the array along the first axis (rows).
✗ Incorrect
The array has only 2 rows (indices 0 and 1). Accessing row 2 is out of bounds, causing IndexError.
🚀 Application
expert3:00remaining
Extracting a submatrix using 2D slicing
Given the array below, which option correctly extracts the 2x2 submatrix from rows 1 to 2 and columns 0 to 1?
NumPy
import numpy as np arr = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]])
Attempts:
2 left
💡 Hint
Remember slicing includes the start index but excludes the end index.
✗ Incorrect
arr[1:3, 0:2] selects rows 1 and 2, columns 0 and 1, giving a 2x2 submatrix.