Slicing rows and columns in NumPy - Time & Space Complexity
We want to understand how the time to slice parts of a numpy array changes as the array gets bigger.
Specifically, how does selecting rows or columns affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.rand(1000, 1000)
# Slice first 10 rows and all columns
slice_rows = arr[:10, :]
# Slice all rows and first 10 columns
slice_cols = arr[:, :10]
This code creates a large 2D array and slices a small part of rows or columns from it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adjusting array metadata (shape, strides, base pointer).
- How many times: Constant time; no traversal or copying of elements.
Slicing creates a view (not a copy), so time is independent of array size or slice size.
| Input Size (n x n) | Slice Time |
|---|---|
| 10 x 10 | O(1) |
| 100 x 100 | O(1) |
| 1000 x 1000 | O(1) |
Pattern observation: Constant time regardless of array or slice dimensions.
Time Complexity: O(1)
Slicing returns a view sharing the same data; only metadata is updated in constant time.
[X] Wrong: "Slicing copies elements, so time grows with slice size (e.g., O(n))."
[OK] Correct: Numpy slicing creates a view without copying data; access is lazy until modification.
Knowing slicing is O(1) helps optimize data pipelines; use .copy() explicitly only when needed.
"What if we slice a square block of size k x k (k = n/2)? How would the time complexity change?"
Answer: Still O(1); view creation is always constant time.
Practice
array[1:3, 0:2] do on a 2D NumPy array?Solution
Step 1: Understand row slicing
This means select rows starting at index 1 up to but not including index 3, so rows 1 and 2.1:3Step 2: Understand column slicing
This means select columns starting at index 0 up to but not including index 2, so columns 0 and 1.0:2Final Answer:
Selects rows 1 and 2, and columns 0 and 1 -> Option AQuick Check:
Slice end is excluded = A [OK]
- Including the end index in the slice
- Confusing rows and columns order
- Using commas incorrectly in slicing
arr?Solution
Step 1: Identify rows and columns in slicing
The first index before comma is rows, second is columns.Step 2: Select all rows and columns 2 to 4
All rows means:, columns 2 to 4 means2:5because end is excluded.Final Answer:
arr[:, 2:5]-> Option BQuick Check:
All rows = :, columns 2 to 4 = 2:5 [OK]
- Swapping rows and columns in slicing
- Using wrong end index in slice
- Missing colon for all rows or columns
import numpy as np
arr = np.array([[10, 20, 30, 40],
[50, 60, 70, 80],
[90, 100, 110, 120]])
print(arr[0:2, 1:3])What is the output?
Solution
Step 1: Slice rows 0 to 1
Rows 0 and 1 are selected (indices 0 and 1), so first two rows.Step 2: Slice columns 1 to 2
Columns 1 and 2 are selected (indices 1 and 2), so second and third columns.Final Answer:
[[20 30] [60 70]] -> Option DQuick Check:
Rows 0-1 and columns 1-2 slice = A [OK]
- Including the end index in slice
- Mixing up rows and columns
- Printing the whole array instead of slice
data?
slice = data[1:3, 0:2]
Solution
Step 1: Examine row slicing
In Python/NumPy slicing, the end index is exclusive, so1:31:3selects rows 1 and 2 only, not row 3.Step 2: Column slicing
Selects columns 0 and 1 (excludes 2).0:2Final Answer:
It selects rows 1 and 2 only, not row 3 -> Option AQuick Check:
End index excluded in slicing = A [OK]
- Assuming slice includes end index
- Ignoring variable naming conflicts
- Confusing rows and columns in slicing
arr. How do you extract the middle 3 rows and middle 3 columns as a new array?Solution
Step 1: Identify middle rows in 5x5 array
Rows are indexed 0 to 4. Middle 3 rows are indices 1, 2, 3, so slice 1:4.Step 2: Identify middle columns similarly
Columns 1, 2, 3 are middle 3 columns, so slice 1:4.Final Answer:
arr[1:4, 1:4]-> Option CQuick Check:
Middle 3 rows and columns = 1:4 slice [OK]
- Using wrong slice end index
- Selecting too few or too many rows/columns
- Confusing zero-based indexing
Ready to prove it?
Pass the test and this pattern moves unknown
