🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

Fancy Indexing

AI & DATA SCIENCE // fancy-indexing

Fancy indexing selects specific elements from an array using a list or array of integer indices, in exactly the order you specify, rather than a contiguous slice.

Syntax

arr[[i1, i2, i3]]
matrix[[r1, r2], [c1, c2]]

Deep Dive Course

Passing a list or array of indices instead of a slice lets you select an arbitrary, non-contiguous, and even repeated or reordered set of elements — indexing with [3, 0, 3, 1] pulls out the elements at positions 3, 0, 3 again, and 1, in exactly that order, something a basic slice can't express. For a 2D array, passing two index arrays, one per dimension, selects specific (row, column) pairs element-wise, rather than a rectangular block — the two arrays are paired up positionally, not crossed together. Unlike basic slicing, fancy indexing always returns a new copy of the selected data.

1Understanding Fancy Indexing

Passing a list or array of indices instead of a slice lets you select an arbitrary, non-contiguous, and even repeated or reordered set of elements — indexing with [3, 0, 3, 1] pulls out the elements at positions 3, 0, 3 again, and 1, in exactly that order, something a basic slice can't express. For a 2D array, passing two index arrays, one per dimension, selects specific (row, column) pairs element-wise, rather than a rectangular block — the two arrays are paired up positionally, not crossed together. Unlike basic slicing, fancy indexing always returns a new copy of the selected data.

💡

For 2D fancy indexing, passing two separate index arrays selects specific (row, column) pairs one at a time, not a rectangular sub-block — use np.ix_() instead when you actually want every combination of a set of rows with a set of columns.

editor.html
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])
localhost:3000

2Practical Example

Here is a real-world application of Fancy Indexing showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.arange(12).reshape(3, 4)
print(matrix[[0, 1], [1, 3]])
localhost:3000

3Best Practices

Follow these guidelines when working with Fancy Indexing:

1. Use fancy indexing to select or reorder specific, non-contiguous elements, instead of building the result with a Python loop

2. Remember fancy indexing always returns a copy, unlike basic slicing, so modifying the result never affects the original array

3. Use np.ix_() when you want a rectangular selection of specific rows crossed with specific columns, rather than paired (row, column) coordinates

⚠️

Tip: For 2D fancy indexing, passing two separate index arrays selects specific (row, column) pairs one at a time, not a rectangular sub-block — use np.ix_() instead when you actually want every combination of a set of rows with a set of columns.

editor.html
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])
Example 02Advanced Example
import numpy as np

matrix = np.arange(12).reshape(3, 4)
print(matrix[[0, 1], [1, 3]])

Best Practices

  • Use fancy indexing to select or reorder specific, non-contiguous elements, instead of building the result with a Python loop
  • Remember fancy indexing always returns a copy, unlike basic slicing, so modifying the result never affects the original array
  • Use np.ix_() when you want a rectangular selection of specific rows crossed with specific columns, rather than paired (row, column) coordinates

Interview Question

For a 2D array, why does indexing with two lists like [0, 1] and [2, 3] select only two elements, rather than a 2x2 block of four?

Hint: Think about how the row-index array and column-index array are paired up.

When you pass two separate index arrays to fancy indexing, NumPy pairs them up element-wise, coordinate by coordinate, rather than treating them as independent lists of rows and columns to cross-combine. So indexing with row list [0, 1] and column list [2, 3] selects exactly the two specific coordinates (0, 2) and (1, 3), producing a 1D result of two values. To select every combination of a set of rows crossed with a set of columns instead, forming an actual rectangular sub-block, you'd wrap the index arrays in np.ix_() first.

Exercises

MediumPractice using Fancy Indexing in a real scenario.
View Solution
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])

Frequently Asked Questions

For a 2D array, why does indexing with two lists like [0, 1] and [2, 3] select only two elements, rather than a 2x2 block of four?

When you pass two separate index arrays to fancy indexing, NumPy pairs them up element-wise, coordinate by coordinate, rather than treating them as independent lists of rows and columns to cross-combine. So indexing with row list [0, 1] and column list [2, 3] selects exactly the two specific coordinates (0, 2) and (1, 3), producing a 1D result of two values. To select every combination of a set of rows crossed with a set of columns instead, forming an actual rectangular sub-block, you'd wrap the index arrays in np.ix_() first.

Related Functions

basic-slicingboolean-indexingnp-ix