0
0
NumPydata~20 mins

2D array indexing (row, col) in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
2D Array Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A40
B60
C50
D90
Attempts:
2 left
💡 Hint
Remember that indexing starts at 0 and the first index is the row, second is the column.
data_output
intermediate
2: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)
A[4 5 6]
B[1 2 3]
C[7 8 9]
D[5 6 7]
Attempts:
2 left
💡 Hint
The colon (:) means all columns in the selected row.
Predict Output
advanced
2: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)
A[10 30]
B[5 40]
C[10 45]
D[15 35]
Attempts:
2 left
💡 Hint
When using two lists for indexing, elements are selected pairwise from each list.
🔧 Debug
advanced
2: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)
AKeyError: 2
BTypeError: list indices must be integers or slices, not tuple
CValueError: too many indices for array
DIndexError: index 2 is out of bounds for axis 0 with size 2
Attempts:
2 left
💡 Hint
Check the size of the array along the first axis (rows).
🚀 Application
expert
3: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]])
Aarr[1:3, 0:2]
Barr[0:2, 1:3]
Carr[1:2, 0:1]
Darr[1:2, 0:2]
Attempts:
2 left
💡 Hint
Remember slicing includes the start index but excludes the end index.