Slicing with start:stop:step in NumPy - Time & Space Complexity
We want to understand how the time it takes to slice a numpy array changes as the array gets bigger.
Specifically, how does using start, stop, and step in slicing affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
sliced = arr[10:900:5]
This code creates an array of 1000 numbers and then slices it from index 10 to 899, taking every 5th element.
- Primary operation: Adjusting metadata (offset, shape, strides) to create a view of the original array. No data copying.
- How many times: Constant time, performed once regardless of array size or slice parameters.
As the array size grows, the slicing operation remains constant time because it creates a view, not a copy.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 (metadata adjustment) |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work is constant, independent of array size, slice length, or step size.
Time Complexity: O(1)
This means the time is constant, as NumPy slicing creates a zero-copy view by adjusting metadata only.
[X] Wrong: "Slicing copies the selected elements, so time proportional to output size (O(k))."
[OK] Correct: NumPy slicing returns a view with adjusted strides—no data is copied. Use .copy() for actual copying, which is O(k).
Knowing slicing is O(1) helps optimize code by avoiding unnecessary copies, crucial for large datasets in data science pipelines.
"What if we changed the step size to 1? How would the time complexity change?"
Answer: Still O(1), as it's always a view.
Practice
step value control in a NumPy array slice like arr[start:stop:step]?Solution
Step 1: Understand the slice syntax
The slicearr[start:stop:step]selects elements fromstartup to but not includingstop, moving bystep.Step 2: Identify the role of
Thestepstepdetermines how many elements to skip between each selected element. For example,step=2picks every second element.Final Answer:
How many elements to skip between each selected element -> Option AQuick Check:
Step controls skipping elements = A [OK]
- Confusing step with start or stop index
- Thinking step is total count of elements
- Ignoring that step can be negative
arr to get every 3rd element from index 1 to 10?Solution
Step 1: Recall slice order
The slice syntax isarr[start:stop:step]. Here, start=1, stop=10, step=3 means from index 1 up to 9, every 3rd element.Step 2: Check each option
arr[1:10:3]matches the correct order and values. Others have wrong order or values that don't match the requirement.Final Answer:
arr[1:10:3] -> Option CQuick Check:
Correct slice syntax = D [OK]
- Mixing up stop and step positions
- Using step as stop index
- Reversing start and stop incorrectly
arr = np.array([10, 20, 30, 40, 50, 60, 70]), what is the output of arr[1:6:2]?Solution
Step 1: Identify slice indices
The slicearr[1:6:2]starts at index 1 (value 20), stops before index 6, stepping by 2.Step 2: Select elements
Indexes selected: 1 (20), 3 (40), 5 (60). So output is [20 40 60].Final Answer:
[20 40 60] -> Option DQuick Check:
Elements at 1,3,5 = [20 40 60] [OK]
- Including stop index element
- Using wrong step size
- Starting from index 0 instead of 1
arr = np.array([1,2,3,4,5])
slice = arr[4:1:-1]What will
slice contain?Solution
Step 1: Understand negative step slicing
Withstep=-1, slicing goes backwards from start index 4 down to stop index 1 (exclusive).Step 2: Identify elements selected
Indexes selected: 4 (5), 3 (4), 2 (3). So slice contains [5 4 3].Final Answer:
[5 4 3] -> Option BQuick Check:
Backward slice from 4 to 2 = [5 4 3] [OK]
- Expecting forward slice with negative step
- Including stop index element
- Thinking it causes error
arr = np.arange(20). How would you slice it to get every 4th element in reverse order starting from the last element?Solution
Step 1: Understand array and goal
arr = np.arange(20)creates array from 0 to 19. We want every 4th element backward starting from last element (index 19).Step 2: Check slicing options
arr[19::-4]starts at index 19, goes backward to start (index 0), stepping by 4, which matches the goal.Step 3: Verify other options
A:arr[::4]every 4th forward [0,4,8,...], wrong direction. C:arr[19:0:4]positive step with start>stop gives empty. D:arr[0:19:-4]start<stop with negative step gives empty.Final Answer:
arr[19::-4] -> Option AQuick Check:
Reverse every 4th from last = arr[19::-4] [OK]
- Using positive step with decreasing indices
- Wrong start or stop indices
- Assuming arr[::-4] always starts at last element
Ready to prove it?
Pass the test and this pattern moves unknown
