Fancy Indexing with Integer Arrays

Fancy indexing is selecting elements by passing an array of integer positions, letting you pick, repeat, and reorder any items in one expression — and it always returns a fresh copy rather than a view.

Learn Fancy Indexing with Integer Arrays in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll select and reorder rows, grab specific (row, column) pairs from a 2D array, assign through fancy indexing, and see exactly how it differs from basic slicing.

Instead of one index like arr[2] , fancy indexing takes a list of indices . NumPy returns the elements at exactly those positions, in that order. You can even repeat a position to duplicate an element.

On a 2D array, a single index list selects whole rows — perfect for reordering or duplicating rows. To pull out specific cells, pass two index arrays : one for rows and one for columns, paired element by element.

You can assign through fancy indexing to update scattered positions in place — no loop needed. But remember: when you read with fancy indexing you get a copy , so modifying that result never touches the original. Basic slicing, by contrast, returns a view that shares memory.

Replace ___ with the index list that reorders the array from largest to smallest.

Answer: [0, 2, 4, 3, 1] — those positions hold 5, 4, 3, 2, 1 in order.

An index in your list is larger than the array allows:

✅ Fix: keep every index between 0 and len(arr) − 1 (negatives count from the end).

❌ Expecting a full grid from two index arrays

✅ Fix: for a sub-grid, index in steps: grid[[0, 1]][:, [0, 1]] .

✅ Fix: assign directly ( arr[[0, 3]] = 0 ) to modify in place.

Given scores and matching names, use argsort plus fancy indexing to print the names of the top three scorers, highest first.

Lesson complete — fancy indexing mastered!

You can select, reorder, and repeat elements with integer arrays, grab exact (row, column) pairs, assign in place, and remember that fancy indexing copies while slicing views.

🚀 Up next: Handling NaN & Infinity — work safely with missing and infinite values.

Practice quiz

What is fancy indexing in NumPy?

  • Selecting with start:stop:step slices
  • Selecting elements with an array of integer positions
  • Selecting with a single integer
  • Selecting with a boolean mask only

Answer: Selecting elements with an array of integer positions. Fancy indexing passes a list or array of integer positions to pick elements.

Does reading with fancy indexing return a copy or a view?

  • Always a copy
  • Always a view
  • A view for 1D, a copy for 2D
  • It depends on the dtype

Answer: Always a copy. Fancy indexing always returns a new copy, unlike basic slicing which returns a view.

For arr = [10, 20, 30, 40, 50], what does arr[[4, 0, 2]] return?

It picks positions 4, 0, 2 in that order: 50, 10, 30.

What shape does the result of fancy indexing follow?

  • The shape of the original array
  • The shape of the index array
  • Always 1D
  • Always 2D

Answer: The shape of the index array. Three indices give three values; the result matches the index array's shape.

For a 3x3 grid, what does grid[[0, 1, 2], [0, 1, 2]] return?

  • A 3x3 block
  • The first row
  • The whole grid

Two index arrays pair element by element, selecting cells (0,0), (1,1), (2,2).

On a 2D array, what does a single index list like grid[[2, 0]] select?

  • Rows 2 then 0
  • Columns 2 then 0
  • Cells (2,0)
  • An error

Answer: Rows 2 then 0. A single index list on a 2D array selects whole rows in that order.

Can you assign to several positions at once with arr[[0, 3]] = 0?

  • No, it raises an error
  • Only on copies
  • Yes, it updates those positions in place
  • Only for floats

Answer: Yes, it updates those positions in place. Assignment through fancy indexing updates the listed positions in place.

Two index arrays like grid[[0,1,2],[0,2,1]] return how many cells?

  • 9 (a full grid)
  • 1
  • 6
  • 3 (paired element by element)

Answer: 3 (paired element by element). They are paired position by position, returning 3 cells, not a 3x3 grid.

What does grid[[0, 2], [2, 0]] return for a 3x3 grid of 0..8?

It selects cells (0,2)=2 and (2,0)=6.

Why does modifying picked = arr[[1, 2]] leave arr unchanged?

  • arr is read-only
  • Fancy indexing read returns a copy
  • The indices were invalid
  • NumPy caches the result

Answer: Fancy indexing read returns a copy. Reading with fancy indexing makes a copy, so edits to it do not touch the original.