0
0
NumPydata~10 mins

Views share memory with originals in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Views share memory with originals
Create original array
Create view from original
Modify view
Original array changes too
Modify original
View reflects original changes
This flow shows how creating a view from a numpy array links their memory, so changes in one affect the other.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
view = arr[1:]
view[0] = 10
print(arr)
Create an array, make a view slice, change the view, and print original to see shared memory effect.
Execution Table
StepActionArray 'arr'View 'view'Notes
1Create arr = [1, 2, 3][1, 2, 3]N/AOriginal array created
2Create view = arr[1:][1, 2, 3][2, 3]View shares memory with arr from index 1
3Modify view[0] = 10[1, 10, 3][10, 3]Change in view updates arr too
4Print arr[1, 10, 3][10, 3]Original array shows updated value
5Modify arr[2] = 20[1, 10, 20][10, 20]Change in arr updates view
6Print view[1, 10, 20][10, 20]View reflects arr's change
7End[1, 10, 20][10, 20]Execution ends
💡 All steps complete; shows how view and original share memory and reflect changes mutually.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
arr[1, 2, 3][1, 2, 3][1, 10, 3][1, 10, 20][1, 10, 20]
viewN/A[2, 3][10, 3][10, 20][10, 20]
Key Moments - 3 Insights
Why does changing 'view[0]' also change 'arr[1]'?
Because 'view' is a view, not a copy, it shares the same memory as 'arr' starting at index 1. So modifying 'view[0]' changes 'arr[1]' as shown in execution_table step 3.
If I change 'arr[2]', why does 'view[1]' change too?
Since 'view' points to the same memory slice of 'arr', changing 'arr[2]' updates 'view[1]' as seen in execution_table step 5.
Is 'view' a separate array with its own data?
No, 'view' is a window into 'arr' data, sharing memory. Changes in one reflect in the other, unlike a copy.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of arr after modifying view[0]?
A[10, 2, 3]
B[1, 2, 3]
C[1, 10, 3]
D[1, 3, 10]
💡 Hint
Check the 'Array arr' column at step 3 in the execution_table.
At which step does changing arr affect the view?
AStep 3
BStep 5
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'View view' columns at step 5 in the execution_table.
If we changed 'view' by making a copy instead of a view, what would happen?
AChanges in view would not affect arr
BChanges in arr would update view
CBoth would update each other
DView would stop existing
💡 Hint
Recall that views share memory but copies do not; see key_moments about shared memory.
Concept Snapshot
Numpy views share memory with the original array.
Creating a view (e.g., arr[1:]) does not copy data.
Modifying the view changes the original array.
Modifying the original array changes the view.
Use views to save memory but be careful with side effects.
Full Transcript
This lesson shows how numpy views share memory with the original array. We start by creating an array 'arr' with values [1, 2, 3]. Then we create a view 'view' by slicing arr from index 1. This view shares the same memory as arr starting at index 1. When we change view[0] to 10, arr[1] also changes to 10, showing the shared memory effect. Later, changing arr[2] to 20 updates view[1] to 20. This means changes in either the view or the original array reflect in the other. Views are not copies but windows into the same data. This is useful for saving memory but requires care to avoid unexpected changes.