0
0
NumPydata~10 mins

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

Choose your learning style9 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.