Assigning Array to Variable

Normal assignments do not make a copy of an array object. Instead, it uses the same ID of the original array to access it. 

Any changes in either get reflected in the other.

Example: Assigning Array to Variable and changing Variable

python3




import numpy as np
  
# creating array
arr = np.array([2, 4, 6, 8, 10])
  
# assigning arr to nc
nc = arr
  
# both arr and nc have same id
print("id of arr", id(arr))
print("id of nc", id(nc))
  
# updating nc
nc[0]= 12
  
# printing the values
print("original array- ", arr)
print("assigned array- ", nc)


Output:

id of arr 26558736
id of nc 26558736
original array-  [12  4  6  8 10]
assigned array-  [12  4  6  8 10]

NumPy Copy and View of Array

While working with NumPy, you might have seen some functions return the copy whereas some functions return the view.

The main difference between copy and view is that the copy is the new array whereas the view is the view of the original array. In other words, it can be said that the copy is physically stored at another location and the view has the same memory location as the original array.

Similar Reads

View of Array in NumPy

The view is just a view of the original ndarray and the view does not own the data....

Copy of Array in NumPy

...

Assigning Array to Variable

The copy is completely a new array and the copy owns the data....

How to check if the Array is a View or a Copy

...

Conclusion

Normal assignments do not make a copy of an array object. Instead, it uses the same ID of the original array to access it....

Contact Us