Advanced NumPy Indexing

Last Updated: 09 Nov 2025


Advanced indexing lets you select irregular, non-contiguous, or conditional elements using lists, boolean masks, or mixed indexingno loops needed.

Hinglish Tip: “Fancy indexing = jaise chaho waise elements nikaalo — list se, condition se, ya dono se!”


1. Integer Array Indexing (Fancy Indexing)

Pass lists/arrays of indices to pick specific elements.

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])

# Pick elements at indices 1, 3, 4
print(arr[[1, 3, 4]])   # [20 40 50]

2D Example

grid = np.array([[10, 20, 30],
                 [40, 50, 60],
                 [70, 80, 90]])

# Pick (0,1), (1,2), (2,0)
rows = [0, 1, 2]
cols = [1, 2, 0]
print(grid[rows, cols])   # [20 60 70]

2. Boolean Indexing (Masking)

Use True/False array same shape → filter matching elements.

data = np.array([25, 78, 92, 45, 88, 33])

mask = data > 70
print("Mask:", mask)
print("High values:", data[mask])   # [78 92 88]

Combine conditions

age = np.array([16, 22, 17, 65, 19])
mask = (age >= 18) & (age <= 60)
print("Eligible:", age[mask])

Warning: Use &, | (not and, or) inside masks.

Modify in-place

age = np.array([16, 22, 17, 65, 19])
age[age < 18] = 18
print(age)

Combine with Fancy

# Pick only even indices where value > 50
idx = np.array([0, 1, 2, 3, 4, 5])
values = np.array([10, 65, 30, 80, 55, 90])

mask = values > 50
print(values[mask][::2])   # [65 90] → every 2nd from filtered

Combine slicing + fancy/boolean.

img = np.array([[1, 2, 3, 4, 5],
                [6, 7, 8, 9, 10],
                [11, 12, 13, 14, 15]])

# Top-left 3×3, but only even columns
print(img[:3, [0, 2, 4]])

3. np.where — Conditional Selection

arr = np.array([10, 20, 30, 40, 50])
print(np.where(arr > 30))   # (array([ 3, 4]),)
print(arr[np.where(arr > 30)])   # [40 50]

4. Modify Using Advanced Indexing

arr = np.array([1, 2, 3, 4, 5])

# Set values at indices 1 and 3 to 99
arr[[1, 3]] = 99
print(arr)   # [1 99 3 99 5]

Set All High Values to Cap

data = np.array([45, 89, 92, 78, 95])
data[data > 90] = 90
print(data)   # [45 89 90 78 90]

5. np.diag — Diagonal elements

  • Give it a 1D array it returns a 2D diagonal matrix
  • Give it a 2D array it extracts the main diagonal (or any diagonal)
import numpy as np

# 1. Create diagonal matrix
d = np.diag([5, 10, 15, 20])
print(d)

# 2. Extract diagonal from any matrix
a = np.arange(16).reshape(4, 4)
print(a)
print("Main diagonal:", np.diag(a))        # [ 0  5 10 15]

# 3. Off-diagonal (k > 0 = above, k < 0 = below)
print("Super-diagonal (k=1):", np.diag(a, k=1))   # [1 6 11]
print("Sub-diagonal   (k=-1):", np.diag(a, k=-1)) # [4 9 14]

6. np.ix_ — Cartesian Indexing

  • Create meshgrid-like index pairs for 2D selection.
  • Clean & fast way to pick any rows × any columns
import numpy as np

big = np.array([[10, 20, 30, 40, 50],
                [60, 70, 80, 90, 100],
                [110, 120, 130, 140, 150],
                [160, 170, 180, 190, 200],
                [210, 220, 230, 240, 250],
                [260, 270, 280, 290, 300]])
print(big)

# Want rows 0, 2, 5 and columns 1, 4
rows = [0, 2, 5]
cols = [1, 4]

data = big[np.ix_(rows, cols)]
print(data)