0
0
NumPydata~10 mins

2D array indexing (row, col) in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the element in the 2D array at row 1, column 2.

NumPy
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60]])
element = arr[[1]]
print(element)
Drag options to blanks, or click blank then click option'
A1, 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[1][2] instead of arr[1, 2]
Using one index instead of two
Mixing up row and column indices
2fill in blank
medium

Complete the code to extract the entire second row from the 2D array.

NumPy
import numpy as np
arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
row = arr[[1]]
print(row)
Drag options to blanks, or click blank then click option'
A0
B2
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 to get the second row
Using arr[:, 1] which gets the second column
Using negative indices incorrectly
3fill in blank
hard

Fix the error in the code to correctly access the element at row 0, column 1.

NumPy
import numpy as np
arr = np.array([[7, 8, 9], [10, 11, 12]])
element = arr[[1]]
print(element)
Drag options to blanks, or click blank then click option'
A[0, 1]
B0, 1
C0][1
D0;1
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiple square brackets like arr[0][1]
Using a list inside brackets like arr[[0,1]]
Using semicolon instead of comma
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 characters.

NumPy
words = ['cat', 'house', 'dog', 'elephant']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword.length > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing the word string directly to a number
Using incorrect attribute like word.length
Not filtering words at all
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 4 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 4
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys
Not filtering words by length
Mixing up keys and values in the dictionary comprehension