Array indexing and slicing in Data Analysis Python - Time & Space Complexity
We want to understand how fast array indexing and slicing run as the data size grows.
How does the time to get or slice parts of an array change when the array gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
value = arr[500] # Indexing
sub_arr = arr[100:200] # Slicing
This code gets one element by index and then gets a slice (view) of the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing one element by index and creating a slice view.
- How many times: Both indexing and slicing are single constant-time operations (no loops).
Getting one element by index takes the same time no matter the array size.
Getting a slice takes constant time no matter the array size or slice length (creates a view).
| Input Size (n) | Approx. Operations for Indexing | Approx. Operations for Slicing |
|---|---|---|
| 10 | 1 | 1 |
| 100 | 1 | 1 |
| 1000 | 1 | 1 |
Pattern observation: Both indexing and slicing times stay constant regardless of array or slice size.
Time Complexity: O(1) for indexing, O(1) for slicing
Both indexing and slicing are constant time no matter the array size; NumPy slicing creates a view without copying data.
[X] Wrong: "Slicing an array copies data and takes O(k) time where k is slice length."
[OK] Correct: NumPy basic slicing creates a view sharing memory with the original, so it's O(1) like indexing.
Knowing how indexing and slicing scale helps you write efficient data code and answer questions about data access speed clearly.
"What if we slice the entire array instead of a small part? How would the time complexity change? (Hint: still O(1))"
Practice
arr[2] do when arr is a Python list?Solution
Step 1: Understand list indexing
In Python, list indexing starts at 0, so index 2 means the third element.Step 2: Identify what
This expression returns the single element at position 2, not a range or removal.arr[2]returnsFinal Answer:
Returns the element at index 2 (third element) of the list -> Option DQuick Check:
Index 2 means third element [OK]
- Thinking slicing syntax is used for single element
- Confusing index 2 with first two elements
- Assuming it removes an element
lst from index 1 to 4 (excluding 4)?Solution
Step 1: Recall Python slice syntax
Slicing uses square brackets with colon:start:stop, where stop is excluded.Step 2: Check each option
Onlylst[1:4]uses correct slice syntax to get elements at indices 1, 2, 3.Final Answer:
lst[1:4] -> Option BQuick Check:
Slice syntax uses colon [OK]
- Using comma instead of colon
- Using parentheses instead of brackets
- Using dash instead of colon
arr = [10, 20, 30, 40, 50], what is the output of arr[1:4]?Solution
Step 1: Understand slicing indices
The slicearr[1:4]includes elements from index 1 up to but not including index 4.Step 2: Extract elements at indices 1, 2, 3
These are 20 (index 1), 30 (index 2), and 40 (index 3).Final Answer:
[20, 30, 40] -> Option AQuick Check:
Slice excludes stop index [OK]
- Including element at stop index
- Confusing indices with values
- Off-by-one errors in slicing
arr = [1, 2, 3, 4, 5] print(arr[2:6])
Solution
Step 1: Understand slicing behavior with out-of-range stop
Python slicing does not raise error if stop index is beyond list length; it stops at the list end.Step 2: Check what
It returns elements from index 2 to the end (index 4), which are [3, 4, 5].arr[2:6]returnsFinal Answer:
It will print elements from index 2 to 5 without error -> Option CQuick Check:
Slicing stop can exceed length safely [OK]
- Expecting IndexError for slicing beyond length
- Thinking slice includes stop index
- Confusing slicing with indexing
data = [5, 10, 15, 20, 25, 30]. How do you get every second element starting from index 1 up to index 5?Solution
Step 1: Understand slicing with step
Slicing syntax isstart:stop:step. To get every second element, step=2.Step 2: Identify correct start and stop
Start is 1, stop is 6 to include element at index 5 (stop excluded).Step 3: Verify the slice
data[1:6:2]returns elements at indices 1, 3, 5 -> [10, 20, 30].Final Answer:
data[1:6:2] -> Option AQuick Check:
Use step in slice for skipping elements [OK]
- Using wrong stop index excluding last element
- Omitting step and getting all elements
- Confusing step with start or stop
Ready to prove it?
Pass the test and this pattern moves unknown
