0
0
NumPydata~10 mins

Combining fancy and slice indexing in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Combining fancy and slice indexing
Start with array
Apply slice indexing
Apply fancy indexing on slice
Get final selected elements
Output result
We first select a slice of the array, then apply fancy indexing on that slice to pick specific elements.
Execution Sample
NumPy
import numpy as np
arr = np.arange(10)
slice_part = arr[2:8]
result = slice_part[[1,3,4]]
print(result)
This code takes a slice of the array from index 2 to 7, then selects elements at positions 1, 3, and 4 from that slice.
Execution Table
StepOperationArray StateIndexing UsedResult/Output
1Create array[0 1 2 3 4 5 6 7 8 9]NoneFull array created
2Slice arr[2:8][2 3 4 5 6 7]Slice 2:8Slice part selected
3Fancy index slice_part[[1,3,4]][3 5 6]Fancy [1,3,4]Elements at positions 1,3,4 from slice
4Print resultN/AN/A[3 5 6]
5EndN/AN/AExecution complete
💡 Finished selecting elements using combined slice and fancy indexing
Variable Tracker
VariableStartAfter Step 2After Step 3Final
arrNone[0 1 2 3 4 5 6 7 8 9][0 1 2 3 4 5 6 7 8 9][0 1 2 3 4 5 6 7 8 9]
slice_partNone[2 3 4 5 6 7][2 3 4 5 6 7][2 3 4 5 6 7]
resultNoneNone[3 5 6][3 5 6]
Key Moments - 2 Insights
Why does the fancy indexing use positions relative to the slice, not the original array?
Because fancy indexing is applied after slicing, the indices refer to positions inside the sliced array, as shown in execution_table step 3.
Can we combine fancy indexing before slicing in the same expression?
No, numpy applies slice indexing first, then fancy indexing on the result, so the order matters as shown in the flow and execution steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what elements does fancy indexing select from the slice?
A[1 3 4]
B[2 4 5]
C[3 5 6]
D[2 3 4]
💡 Hint
Check the 'Result/Output' column at step 3 in the execution_table.
At which step does the slice_part variable get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the variable_tracker and execution_table to see when slice_part is assigned.
If we changed the fancy index to [0,2], what would the result be?
A[2 4]
B[3 6]
C[2 3]
D[4 5]
💡 Hint
Refer to the slice_part array [2 3 4 5 6 7] and pick elements at positions 0 and 2.
Concept Snapshot
Combining fancy and slice indexing:
- First slice the array: arr[start:stop]
- Then apply fancy indexing on the slice: slice_part[[indices]]
- Fancy indices refer to positions inside the slice
- Result is elements selected by fancy indices from the slice
- Useful to pick specific elements from a subarray
Full Transcript
This visual execution shows how numpy combines slice and fancy indexing. We start with an array from 0 to 9. We slice it from index 2 to 7, getting elements [2 3 4 5 6 7]. Then we apply fancy indexing with indices [1,3,4] on this slice, selecting elements at those positions inside the slice, which are [3 5 6]. The final output is printed. Variables arr, slice_part, and result change as shown. Key points are that fancy indexing applies after slicing, so indices are relative to the slice, not the original array. This helps pick specific elements from a subarray easily.