0
0
NumPydata~20 mins

Single element access in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single Element Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Accessing a single element in a 2D NumPy array

What is the output of the following code?

NumPy
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60]])
result = arr[1, 2]
print(result)
A60
B[60]
C50
DIndexError
Attempts:
2 left
💡 Hint

Remember that indexing starts at 0 and you access elements by row and column.

data_output
intermediate
2:00remaining
Extracting a single element from a 3D NumPy array

Given the 3D array below, what is the value of arr[0, 1, 2]?

NumPy
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
A9
B5
C6
DIndexError
Attempts:
2 left
💡 Hint

Think of the array as layers, rows, and columns. The first index selects the layer.

🔧 Debug
advanced
2:00remaining
Identify the error in single element access

What error will the following code raise?

NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr[3]
print(result)
AIndexError: index 3 is out of bounds for axis 0 with size 3
BTypeError: list indices must be integers or slices
CValueError: too many indices for array
DNo error, output is 3
Attempts:
2 left
💡 Hint

Check the size of the array and the index used.

🚀 Application
advanced
2:00remaining
Accessing a single element in a masked NumPy array

Given the masked array below, what is the output of masked_arr[1]?

NumPy
import numpy as np
import numpy.ma as ma
arr = np.array([10, 20, 30])
masked_arr = ma.masked_array(arr, mask=[False, True, False])
print(masked_arr[1])
AValueError
B20
Cmasked
D--
Attempts:
2 left
💡 Hint

Masked elements show as '--' when printed.

🧠 Conceptual
expert
2:00remaining
Understanding single element access with fancy indexing

What is the output of the following code?

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
result = arr[[1]][0]
print(result)
ATypeError
B20
CIndexError
D[20]
Attempts:
2 left
💡 Hint

Remember that arr[[1]] returns a 1-element array, then [0] accesses the first element.