2D array indexing (row, col) in NumPy - Time & Space Complexity
We want to understand how long it takes to get a value from a 2D array using row and column indexes.
How does the time needed change when the array gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.rand(1000, 1000) # Create a 2D array with 1000 rows and 1000 columns
value = arr[500, 700] # Access the element at row 500, column 700
This code creates a large 2D array and accesses one element by specifying its row and column.
- Primary operation: Accessing a single element by its row and column index.
- How many times: Exactly once for this access.
Getting one element from a 2D array takes the same time no matter how big the array is.
| Input Size (n x n) | Approx. Operations |
|---|---|
| 10 x 10 | 1 |
| 100 x 100 | 1 |
| 1000 x 1000 | 1 |
Pattern observation: The time to access one element stays the same even if the array grows larger.
Time Complexity: O(1)
This means accessing an element by row and column is done in constant time, no matter the array size.
[X] Wrong: "Accessing an element in a big 2D array takes longer because the array is large."
[OK] Correct: The computer calculates the element's position directly using the indexes, so it does not need to look through other elements.
Knowing that 2D array indexing is fast helps you explain why some algorithms are efficient and others are slow. It shows you understand how data is stored and accessed.
"What if we tried to find a value by searching every element in the 2D array? How would the time complexity change?"
Practice
arr[2, 1] mean when arr is a 2D numpy array?Solution
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.Step 2: Interpret the indexing
arr[2, 1]accesses the element at row 3, column 2.Final Answer:
Access the element in the 3rd row and 2nd column -> Option AQuick Check:
Row 2, Col 1 means 3rd row, 2nd column [OK]
- Confusing row and column order
- Starting count from 1 instead of 0
- Thinking arr[2,1] returns a row or column
arr?Solution
Step 1: Recall zero-based indexing
The 1st row is index 0, and the 3rd column is index 2.Step 2: Match indexes to syntax
So,arr[0, 2]accesses the element in the 1st row and 3rd column.Final Answer:
arr[0, 2] -> Option BQuick Check:
Row 0, Col 2 = 1st row, 3rd column [OK]
- Using 1-based indexes like arr[1,3]
- Swapping row and column indexes
- Using parentheses instead of brackets
import numpy as np arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) print(arr[1, 2])
What is the output?
Solution
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.Step 2: Confirm the printed value
The code printsarr[1, 2], so the output is 30.Final Answer:
30 -> Option AQuick Check:
arr[1,2] = 30 [OK]
- Choosing element from wrong row or column
- Confusing zero-based indexing
- Reading the array incorrectly
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr[2, 0])
Solution
Step 1: Check array shape and indexes
The array has 2 rows (index 0 and 1). Index 2 for row is out of range.Step 2: Understand error type
Accessingarr[2, 0]causes an IndexError because row 2 does not exist.Final Answer:
IndexError because row 2 does not exist -> Option CQuick Check:
Row index 2 is invalid for 2-row array [OK]
- Assuming indexing starts at 1
- Ignoring array shape
- Confusing error types
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?Solution
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.Step 2: Check each option
arr[3, 1] = 100 usesarr[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.Final Answer:
arr[3, 1] = 100 -> Option DQuick Check:
Last row = 3, second column = 1 [OK]
- Using out-of-range indexes like 4
- Confusing negative indexes for columns
- Mixing row and column positions
Ready to prove it?
Pass the test and this pattern moves unknown
