Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does combining fancy indexing and slice indexing in NumPy allow you to do?
arr[[0, 2], 1:4] selects:
easy
A. Rows 0 to 2, columns 1 and 4
B. Rows 1 to 4, columns 0 and 2
C. Rows 0 and 2, columns 1 to 3
D. Rows 1 and 4, columns 0 to 2

Solution

  1. Step 1: Understand fancy indexing for rows

    The fancy index [0, 2] selects rows 0 and 2 specifically.
  2. Step 2: Understand slice indexing for columns

    The slice 1:4 selects columns starting at 1 up to but not including 4, so columns 1, 2, and 3.
  3. Final Answer:

    Rows 0 and 2, columns 1 to 3 -> Option C
  4. Quick Check:

    Fancy rows + slice columns = selected subarray [OK]
Hint: Fancy picks rows, slice picks continuous columns [OK]
Common Mistakes:
  • Confusing rows and columns in indexing
  • Forgetting slice end is exclusive
  • Mixing up fancy and slice roles
2. Which of the following is the correct syntax to select rows 1 and 3 and columns 2 to 4 from a NumPy array arr?
easy
A. arr[[1, 3], 2:5]
B. arr[1:3, 2:5]
C. arr[1, 3, 2:5]
D. arr[[1, 3], [2:5]]

Solution

  1. Step 1: Identify fancy indexing for rows

    To select rows 1 and 3, use fancy indexing with a list: [1, 3].
  2. Step 2: Use slice for columns

    To select columns 2 to 4, use slice 2:5 (end exclusive).
  3. Final Answer:

    arr[[1, 3], 2:5] -> Option A
  4. Quick Check:

    Fancy rows + slice columns syntax correct [OK]
Hint: Fancy indexing uses lists, slice uses colon syntax [OK]
Common Mistakes:
  • Using commas inside fancy index list
  • Using slice inside list brackets
  • Incorrect slice end index
3. Given the array:
import numpy as np
arr = np.array([[10, 20, 30, 40],
                [50, 60, 70, 80],
                [90, 100, 110, 120],
                [130, 140, 150, 160]])
print(arr[[1, 3], 1:3])

What is the output?
medium
A. [[ 70 80] [150 160]]
B. [[ 60 70] [140 150]]
C. [[ 50 60] [130 140]]
D. [[ 60 70 80] [140 150 160]]

Solution

  1. Step 1: Select rows using fancy indexing

    Rows 1 and 3 are:
    [50, 60, 70, 80]
    [130, 140, 150, 160]
  2. Step 2: Select columns 1 to 2 (slice 1:3)

    Columns 1 and 2 for these rows are:
    Row 1: 60, 70
    Row 3: 140, 150
  3. Final Answer:

    [[ 60 70] [140 150]] -> Option B
  4. Quick Check:

    Fancy rows + slice columns output matches [OK]
Hint: Pick rows first, then slice columns [OK]
Common Mistakes:
  • Selecting wrong rows or columns
  • Including column 3 by mistake
  • Confusing row and column indices
4. What is wrong with this code snippet?
import numpy as np
arr = np.arange(16).reshape(4,4)
result = arr[1:3, [0, 2]]
print(result)
medium
A. It prints rows 1 and 2 with columns 0 and 2 correctly
B. It raises an error because slice and fancy indexing cannot be combined
C. It prints rows 0 and 2 with columns 1 and 3 incorrectly
D. It raises an error because the fancy index list is invalid

Solution

  1. Step 1: Understand slice and fancy indexing combination

    Slice 1:3 selects rows 1 and 2. Fancy index [0, 2] selects columns 0 and 2.
  2. Step 2: Check if code runs and output

    This combination is valid in NumPy and prints the selected subarray without error.
  3. Final Answer:

    It prints rows 1 and 2 with columns 0 and 2 correctly -> Option A
  4. Quick Check:

    Slice rows + fancy columns works fine [OK]
Hint: Slice and fancy indexing can combine on different axes [OK]
Common Mistakes:
  • Thinking slice and fancy indexing cannot mix
  • Confusing which axis uses slice or fancy
  • Expecting an error when code is valid
5. You have a 5x5 NumPy array arr. You want to select rows 0, 2, and 3, but only columns 1 to 3 inclusive. Which code correctly does this?
hard
A. arr[1:4, [0, 2, 4]]
B. arr[0:5:2, 1:4]
C. arr[[0, 2, 4], [1, 2, 3]]
D. arr[[0, 2, 3], 1:4]

Solution

  1. Step 1: Identify fancy indexing for rows

    Rows 0, 2, and 3 are selected using fancy indexing: [0, 2, 3].
  2. Step 2: Use slice for columns 1 to 3 inclusive

    Slice 1:4 selects columns 1, 2, and 3 (end index exclusive).
  3. Final Answer:

    arr[[0, 2, 3], 1:4] -> Option D
  4. Quick Check:

    Fancy rows + slice columns selects correct subarray [OK]
Hint: Fancy list for rows, slice for continuous columns [OK]
Common Mistakes:
  • Using slice for rows instead of fancy list
  • Using fancy list for columns incorrectly
  • Off-by-one error in slice end index
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard