Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does arr[2, 1] mean when arr is a 2D numpy array?
easy
A. Access the element in the 3rd row and 2nd column
B. Access the element in the 2nd row and 3rd column
C. Access the entire 2nd row
D. Access the entire 1st column

Solution

  1. Step 1: Understand zero-based indexing

    In numpy, counting starts at 0, so index 2 means the 3rd row, and index 1 means the 2nd column.
  2. Step 2: Interpret the indexing

    arr[2, 1] accesses the element at row 3, column 2.
  3. Final Answer:

    Access the element in the 3rd row and 2nd column -> Option A
  4. Quick Check:

    Row 2, Col 1 means 3rd row, 2nd column [OK]
Hint: Remember: rows and columns start counting at zero [OK]
Common Mistakes:
  • Confusing row and column order
  • Starting count from 1 instead of 0
  • Thinking arr[2,1] returns a row or column
2. Which of the following is the correct syntax to access the element in the 1st row and 3rd column of a numpy 2D array arr?
easy
A. arr[1, 3]
B. arr[0, 2]
C. arr[3, 1]
D. arr[2, 0]

Solution

  1. Step 1: Recall zero-based indexing

    The 1st row is index 0, and the 3rd column is index 2.
  2. Step 2: Match indexes to syntax

    So, arr[0, 2] accesses the element in the 1st row and 3rd column.
  3. Final Answer:

    arr[0, 2] -> Option B
  4. Quick Check:

    Row 0, Col 2 = 1st row, 3rd column [OK]
Hint: Count rows and columns from zero for correct indexing [OK]
Common Mistakes:
  • Using 1-based indexes like arr[1,3]
  • Swapping row and column indexes
  • Using parentheses instead of brackets
3. Given the numpy array:
import numpy as np
arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(arr[1, 2])

What is the output?
medium
A. 30
B. 25
C. 20
D. 45

Solution

  1. Step 1: Identify the element at row 1, column 2

    Row 1 is the second row: [20, 25, 30]. Column 2 is the third element in that row, which is 30.
  2. Step 2: Confirm the printed value

    The code prints arr[1, 2], so the output is 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    arr[1,2] = 30 [OK]
Hint: Remember row then column to find the element [OK]
Common Mistakes:
  • Choosing element from wrong row or column
  • Confusing zero-based indexing
  • Reading the array incorrectly
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr[2, 0])
medium
A. Prints 3 correctly
B. SyntaxError due to wrong brackets
C. IndexError because row 2 does not exist
D. Prints 1 correctly

Solution

  1. Step 1: Check array shape and indexes

    The array has 2 rows (index 0 and 1). Index 2 for row is out of range.
  2. Step 2: Understand error type

    Accessing arr[2, 0] causes an IndexError because row 2 does not exist.
  3. Final Answer:

    IndexError because row 2 does not exist -> Option C
  4. Quick Check:

    Row index 2 is invalid for 2-row array [OK]
Hint: Check array shape before indexing to avoid errors [OK]
Common Mistakes:
  • Assuming indexing starts at 1
  • Ignoring array shape
  • Confusing error types
5. You have a 4x4 numpy array arr filled with numbers 1 to 16 in row-major order. Which code correctly changes the element in the last row and second column to 100?
hard
A. arr[3, -2] = 100
B. arr[4, 2] = 100
C. arr[-1, 2] = 100
D. arr[3, 1] = 100

Solution

  1. Step 1: Identify last row and second column indexes

    For a 4x4 array, rows and columns are indexed 0 to 3. Last row is index 3, second column is index 1.
  2. Step 2: Check each option

    arr[3, 1] = 100 uses arr[3, 1] which is correct. arr[4, 2] = 100 uses invalid index 4. arr[-1, 2] = 100 uses column index 2 (third column), not second. arr[3, -2] = 100 uses negative index -2 for column which is the third column, not second.
  3. Final Answer:

    arr[3, 1] = 100 -> Option D
  4. Quick Check:

    Last row = 3, second column = 1 [OK]
Hint: Use zero-based indexes: last row is size-1, second column is 1 [OK]
Common Mistakes:
  • Using out-of-range indexes like 4
  • Confusing negative indexes for columns
  • Mixing row and column positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard