Bird
Raised Fist0
NumPydata~10 mins

Fancy indexing with integer arrays in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Fancy indexing with integer arrays
Start with base array
↓
Provide integer index array
↓
Use index array to select elements
↓
Create new array with selected elements
↓
Output
Fancy indexing uses an array of integers to pick elements from another array, creating a new array with those elements.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = np.array([1, 3, 4])
result = arr[indices]
print(result)
Select elements at positions 1, 3, and 4 from the array using fancy indexing.
Execution Table
StepActionArray StateIndex ArrayResultExplanation
1Create base array[10, 20, 30, 40, 50]--Base array with 5 elements created
2Create index array-[1, 3, 4]-Index array with positions to select
3Select elements using indices[10, 20, 30, 40, 50][1, 3, 4][20, 40, 50]Elements at positions 1,3,4 selected
4Print result--[20, 40, 50]Output the selected elements
💡 All indices processed, fancy indexing completed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrundefined[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
indicesundefinedundefined[1, 3, 4][1, 3, 4][1, 3, 4]
resultundefinedundefinedundefined[20, 40, 50][20, 40, 50]
Key Moments - 3 Insights
Why does the result array have length 3, not the same length as the base array?
Because fancy indexing selects elements only at the positions given by the index array, which has length 3 (see execution_table step 3). The result length matches the index array length.
What happens if an index is repeated in the index array?
The element at that repeated index appears multiple times in the result. Fancy indexing picks elements in order, so repeats cause repeats in output (not shown here but follows same logic).
Can the index array contain indices out of range?
No, if an index is out of range, numpy raises an IndexError. All indices must be valid positions in the base array.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the value of result?
A[20, 40, 50]
B[1, 3, 4]
C[10, 30, 50]
D[10, 20, 30, 40, 50]
💡 Hint
Check the 'Result' column in step 3 of execution_table
At which step is the index array created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column and when 'Create index array' happens
If the index array was [0, 0, 2], what would be the length of the result array?
A2
B3
C5
D1
💡 Hint
Result length matches index array length, see variable_tracker for indices length
Concept Snapshot
Fancy indexing with integer arrays:
- Use an integer array to pick elements from another array.
- Syntax: result = arr[indices]
- Result length equals index array length.
- Repeated indices repeat elements in result.
- Indices must be valid positions in base array.
Full Transcript
Fancy indexing with integer arrays means using an array of integers as positions to select elements from another array. We start with a base array, then create an index array with positions we want. Using arr[indices], numpy picks elements at those positions and creates a new array. The result length matches the index array length. If indices repeat, elements repeat in the result. Indices must be valid or an error occurs. This method is useful to select multiple elements at once in any order.

Practice

(1/5)
1. What does fancy indexing with integer arrays in NumPy allow you to do?
easy
A. Create a new array filled with zeros
B. Sort an array in ascending order
C. Select multiple elements from an array using a list of positions
D. Change the data type of an array

Solution

  1. Step 1: Understand fancy indexing

    Fancy indexing uses a list or array of integers to pick elements by their positions.
  2. Step 2: Identify the correct operation

    It selects multiple elements from the original array based on given indices.
  3. Final Answer:

    Select multiple elements from an array using a list of positions -> Option C
  4. Quick Check:

    Fancy indexing = select elements by positions [OK]
Hint: Fancy indexing picks elements by position list [OK]
Common Mistakes:
  • Confusing fancy indexing with sorting
  • Thinking it changes data type
  • Assuming it creates arrays of zeros
2. Which of the following is the correct syntax to select elements at positions 0, 2, and 4 from a NumPy array arr using fancy indexing?
easy
A. arr{0, 2, 4}
B. arr(0, 2, 4)
C. arr[0:2:4]
D. arr[[0, 2, 4]]

Solution

  1. Step 1: Recall fancy indexing syntax

    Fancy indexing uses double square brackets with a list or array of indices inside.
  2. Step 2: Match syntax to options

    Only arr[[0, 2, 4]] uses correct double brackets and list format.
  3. Final Answer:

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

    Double brackets with list = correct fancy indexing [OK]
Hint: Use double brackets with list for fancy indexing [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using curly braces which are invalid
  • Using slicing syntax incorrectly
3. What is the output of the following code?
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = np.array([1, 3, 4])
result = arr[indices]
print(result)
medium
A. [20 40 50]
B. [10 30 40]
C. [20 30 40]
D. [50 40 20]

Solution

  1. Step 1: Understand the array and indices

    Original array is [10, 20, 30, 40, 50]. Indices are [1, 3, 4].
  2. Step 2: Select elements at given indices

    Elements at positions 1, 3, 4 are 20, 40, 50 respectively.
  3. Final Answer:

    [20 40 50] -> Option A
  4. Quick Check:

    arr[1,3,4] = [20 40 50] [OK]
Hint: Match indices to array positions to get output [OK]
Common Mistakes:
  • Mixing up indices and values
  • Using wrong positions from array
  • Confusing slicing with fancy indexing
4. Identify the error in the following code snippet:
import numpy as np
arr = np.array([5, 10, 15, 20])
indices = [1, 4]
print(arr[indices])
medium
A. IndexError because index 4 is out of bounds
B. SyntaxError due to wrong brackets
C. TypeError because indices is a list
D. No error, prints [10 20]

Solution

  1. Step 1: Check array size and indices

    Array length is 4, valid indices are 0 to 3. Index 4 is invalid.
  2. Step 2: Understand error type

    Accessing index 4 causes IndexError: index out of bounds.
  3. Final Answer:

    IndexError because index 4 is out of bounds -> Option A
  4. Quick Check:

    Index 4 invalid for length 4 array [OK]
Hint: Check if all indices are within array length [OK]
Common Mistakes:
  • Assuming list indices are always valid
  • Confusing syntax errors with index errors
  • Thinking list indices cause type errors
5. Given a 2D NumPy array arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), how can you use fancy indexing to select elements at positions (0,2), (1,0), and (2,1) and create a new 1D array with these values?
hard
A. arr[[0,2,1], [2,1,0]]
B. arr[[0,1,2], [2,0,1]]
C. arr[[2,1,0], [0,1,2]]
D. arr[[0,1,2]][[2,0,1]]

Solution

  1. Step 1: Understand 2D fancy indexing

    To select elements at (row, col) positions, use two index arrays: one for rows, one for columns.
  2. Step 2: Match given positions to index arrays

    Positions are (0,2), (1,0), (2,1), so rows = [0,1,2], cols = [2,0,1].
  3. Step 3: Use fancy indexing syntax

    Use arr[[0,1,2], [2,0,1]] to get elements [3,4,8].
  4. Final Answer:

    arr[[0,1,2], [2,0,1]] -> Option B
  5. Quick Check:

    Rows and cols arrays match positions [OK]
Hint: Use two index arrays: rows and columns [OK]
Common Mistakes:
  • Mixing row and column indices order
  • Using chained indexing instead of fancy indexing
  • Using wrong index arrays for positions
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