Articles → Numpy → Copy Array By Value And Reference In Numpy
Copy Array By Value And Reference In Numpy
Copy By Reference
import numpy as np
arr = np.arange(0,10)
# Copy by reference
arr_ref = arr
# Printing array
print("Arr:", arr, ", arr_ref:", arr_ref)
# Change in value of arr
arr[:] = 20
# Printing array
print("Arr:", arr, ", arr_ref:", arr_ref)
- We have 2 arrays i.e., "arr" and "arr_ref"
- The reference of "arr" is copied to "arr_ref"
- Change in the value of "arr" is also reflected in "arr_ref"
Click to Enlarge
Copy By Value
import numpy as np
arr = np.arange(0,10)
# Copy by values
arr_ref = arr.copy()
# Printing array
print("Arr:", arr, ", arr_ref:", arr_ref)
# Change in value of arr
arr[:] = 20
# Printing array
print("Arr:", arr, ", arr_ref:", arr_ref)
Click to Enlarge