Bird
Raised Fist0
NumPydata~5 mins

Combining fancy and slice indexing in NumPy

Choose your learning style10 modes available
Introduction

We use combining fancy and slice indexing to pick specific rows or columns and also select ranges of data at the same time. This helps us get exactly the data we want from a big table or array.

You want to select specific rows by their index but also want a continuous range of columns.
You need to extract certain elements from a matrix where rows are chosen by a list and columns by a slice.
You want to analyze parts of data by mixing exact positions and ranges.
You want to quickly grab multiple parts of data without looping.
Syntax
NumPy
array[fancy_index, slice]
# or
array[slice, fancy_index]

Fancy indexing means using a list or array of indices to pick specific elements.

Slice means selecting a continuous range using start:stop:step.

Examples
Select rows 0 and 2, and columns 1 to 2 (slice excludes stop index 3).
NumPy
import numpy as np
arr = np.arange(16).reshape(4,4)
rows = [0, 2]
cols = slice(1, 3)
result = arr[rows, cols]
print(result)
Select rows 2 to 4 and columns 1 and 3.
NumPy
arr = np.arange(20).reshape(5,4)
cols = [1, 3]
rows = slice(2, 5)
result = arr[rows, cols]
print(result)
Sample Program

This program creates a 5x5 grid of numbers. It picks rows 1, 3, and 4 using a list (fancy indexing) and columns 2 to 4 using a slice. Then it prints the original array and the selected part.

NumPy
import numpy as np

# Create a 5x5 array with numbers 0 to 24
arr = np.arange(25).reshape(5, 5)

# Fancy index: select rows 1, 3, and 4
rows = [1, 3, 4]

# Slice: select columns from 2 to 4 (2, 3, 4)
cols = slice(2, 5)

# Combine fancy indexing for rows and slice for columns
selected = arr[rows, cols]

print("Original array:\n", arr)
print("\nSelected rows and columns:\n", selected)
OutputSuccess
Important Notes

When combining fancy and slice indexing, the fancy index always applies to the first dimension you specify.

The result shape depends on the fancy index length and the slice length.

Mixing fancy and slice indexing is faster than looping over elements.

Summary

Fancy indexing uses lists or arrays of indices to pick specific rows or columns.

Slices select continuous ranges of rows or columns.

Combining them lets you pick exact rows and ranges of columns (or vice versa) 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