Bird
Raised Fist0
NumPydata~10 mins

np.take() and np.put() for advanced selection in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - np.take() and np.put() for advanced selection
Start with array
↓
Use np.take() with indices
↓
Extract selected elements
↓
Use np.put() with indices and values
↓
Update array elements at indices
↓
End with modified array
This flow shows how np.take() extracts elements by index, and np.put() updates elements at given indices in an array.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [1, 3]
taken = np.take(arr, indices)
np.put(arr, indices, [99, 88])
print(arr)
This code takes elements at positions 1 and 3, then replaces them with new values.
Execution Table
StepActionArray StateIndicesValuesOutput/Result
1Initialize arr[10, 20, 30, 40, 50]---
2Define indices-[1, 3]--
3np.take(arr, indices)[10, 20, 30, 40, 50][1, 3]-[20, 40]
4np.put(arr, indices, [99, 88])[10, 20, 30, 40, 50][1, 3][99, 88][10, 99, 30, 88, 50]
5Print arr[10, 99, 30, 88, 50]--[10, 99, 30, 88, 50]
6End[10, 99, 30, 88, 50]---
💡 Finished updating array elements at specified indices.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 99, 30, 88, 50][10, 99, 30, 88, 50]
indices-[1, 3][1, 3][1, 3][1, 3]
taken--[20, 40][20, 40][20, 40]
Key Moments - 3 Insights
Why does np.take() return a new array instead of changing the original?
np.take() only extracts elements by index and returns a new array without modifying the original array, as shown in step 3 of the execution_table.
How does np.put() change the original array?
np.put() updates the original array at specified indices with new values, modifying it in place as seen in step 4 where arr changes.
Can indices in np.put() be repeated, and what happens then?
Yes, repeated indices cause multiple updates; the last value assigned to that index remains. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does np.take() output?
A[99, 88]
B[10, 30]
C[20, 40]
D[10, 99, 30, 88, 50]
💡 Hint
Check the Output/Result column at step 3 in the execution_table.
At which step does the original array get modified?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the Array State column to see when arr changes.
If np.put() used indices [1, 1] with values [99, 77], what would arr[1] be after?
A77
BOriginal value 20
C99
DError due to repeated indices
💡 Hint
Repeated indices overwrite in order; last value assigned stays.
Concept Snapshot
np.take(array, indices) extracts elements at given indices without changing the original array.
np.put(array, indices, values) updates elements at given indices in the original array.
Indices can be any valid integer positions.
np.take returns a new array; np.put modifies in place.
Useful for advanced selection and targeted updates.
Full Transcript
This lesson shows how to use numpy's np.take() and np.put() functions for advanced selection and modification of array elements. np.take() extracts elements at specified indices and returns a new array without changing the original. np.put() updates the original array at given indices with new values. The example starts with an array [10, 20, 30, 40, 50], takes elements at indices 1 and 3, then replaces those elements with 99 and 88 respectively. The execution table traces each step, showing the array state and outputs. Key moments clarify that np.take() does not modify the original array, while np.put() does. The visual quiz tests understanding of outputs and array changes. This helps beginners see how to select and update array elements precisely.

Practice

(1/5)
1. What does np.take() do in NumPy?
easy
A. Selects elements from an array using specified indices
B. Changes elements in an array at specified indices
C. Sorts the elements of an array
D. Creates a new array filled with zeros

Solution

  1. Step 1: Understand the function purpose

    np.take() is used to pick elements from an array by their index positions.
  2. Step 2: Compare with other options

    Only Selects elements from an array using specified indices describes selecting elements by indices, while others describe different functions.
  3. Final Answer:

    Selects elements from an array using specified indices -> Option A
  4. Quick Check:

    np.take() = select elements [OK]
Hint: np.take() picks by index, np.put() changes by index [OK]
Common Mistakes:
  • Confusing np.take() with np.put()
  • Thinking np.take() modifies the array
  • Mixing np.take() with sorting functions
2. Which of the following is the correct syntax to replace elements at indices [1, 3] in array a with values [10, 20] using np.put()?
easy
A. np.put(a, [1, 3], [10, 20])
B. np.put([1, 3], a, [10, 20])
C. np.put(a, [10, 20], [1, 3])
D. np.put(a, 10, [1, 3, 20])

Solution

  1. Step 1: Recall np.put() syntax

    The correct syntax is np.put(array, indices, values) to replace elements at given indices.
  2. Step 2: Match syntax with options

    np.put(a, [1, 3], [10, 20]) matches the correct order: array, indices, values.
  3. Final Answer:

    np.put(a, [1, 3], [10, 20]) -> Option A
  4. Quick Check:

    np.put(array, indices, values) [OK]
Hint: np.put(array, indices, values) replaces elements [OK]
Common Mistakes:
  • Swapping the order of arguments
  • Passing values as indices
  • Using wrong data types for indices or values
3. What is the output of the following code?
import numpy as np
a = np.array([5, 10, 15, 20, 25])
indices = [0, 2, 4]
result = np.take(a, indices)
print(result)
medium
A. [10 20 25]
B. [5 15 25]
C. [5 10 15]
D. [0 2 4]

Solution

  1. Step 1: Understand np.take() with given indices

    np.take(a, [0, 2, 4]) selects elements at positions 0, 2, and 4 from array a.
  2. Step 2: Extract elements from array

    Elements at these positions are 5 (index 0), 15 (index 2), and 25 (index 4).
  3. Final Answer:

    [5 15 25] -> Option B
  4. Quick Check:

    np.take() picks elements by index [OK]
Hint: np.take() returns elements at given indices [OK]
Common Mistakes:
  • Confusing indices with values
  • Expecting output as original array
  • Mixing up index positions
4. Identify the error in this code snippet:
import numpy as np
a = np.array([1, 2, 3, 4])
np.put(a, [1, 4], [10, 20])
print(a)
medium
A. Syntax error in np.put() usage
B. Values list length does not match indices length
C. Index 4 is out of bounds for array of size 4
D. np.put() cannot modify arrays in-place

Solution

  1. Step 1: Check array size and indices

    Array a has size 4, valid indices are 0 to 3. Index 4 is invalid.
  2. Step 2: Understand np.put() behavior

    np.put() tries to assign values at given indices. Using index 4 causes an IndexError.
  3. Final Answer:

    Index 4 is out of bounds for array of size 4 -> Option C
  4. Quick Check:

    Index error due to invalid index [OK]
Hint: Check indices are within array size before np.put() [OK]
Common Mistakes:
  • Ignoring array size limits
  • Assuming np.put() auto-expands array
  • Confusing index and value positions
5. Given a = np.array([1, 2, 3, 4, 5]), how can you use np.put() and np.take() together to replace elements at indices [0, 2] with [10, 20] and then retrieve these updated elements?
hard
A. np.take(a, [10, 20]); np.put(a, [0, 2])
B. np.take(a, [0, 2], [10, 20]); np.put(a, [0, 2])
C. np.put([0, 2], a, [10, 20]); np.take([0, 2], a)
D. np.put(a, [0, 2], [10, 20]); np.take(a, [0, 2])

Solution

  1. Step 1: Use np.put() to update elements

    np.put(a, [0, 2], [10, 20]) replaces elements at indices 0 and 2 with 10 and 20.
  2. Step 2: Use np.take() to retrieve updated elements

    np.take(a, [0, 2]) returns the updated elements at those indices.
  3. Final Answer:

    np.put(a, [0, 2], [10, 20]); np.take(a, [0, 2]) -> Option D
  4. Quick Check:

    Put updates, take retrieves updated values [OK]
Hint: Put first to update, then take to get new values [OK]
Common Mistakes:
  • Mixing argument order in np.put()
  • Using np.take() to update values
  • Confusing arrays and indices in functions
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