Skip to main content

NumPy Array: Difference Between Copy and View

Copy in NumPy:

The contents which are physically stored in another location, it is known as Copy. It usually returns the copy of original array which is stored at the new location. This copy doesn't share any memory with original array.

The changes made to copy will not effect original array. Thus, any changes made to the original array will not affect the copy.

Example:

import numpy as np
a=np.array([10,20,30,40,50]) #original array
b=a.copy() #copying the array
a[0]=100 #changing the original array
print(a)
print(b)

Output:

[100  20  30  40  50]
[10 20 30 40 50]

Therefore, changes made in copy are not reflected in the original array.

View in NumPy:

In this the view does not own any data.The changes made to this view will also affect the original array and if any changes made to the original array will affect the view. 
This function returns a view of the original array stored at the existing location.It doesn't have its own data or memory instead it uses original array.

Example:

import numpy as np
a=np.array([10,20,30,40,50])#Original Array
b = a.view()
a[0] = 70 #changing the original array
print(a) 
print(b)

Output:

[70 20 30 40 50]
[70 20 30 40 50]
Submitted by devanshi.srivastava on August 16, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.