Introduction
NumPy provides copy and view methods to duplicate or reference arrays. Understanding the difference between them is important because a copy creates a new array with its own memory, while a view shares memory with the original array. In this chapter, you’ll practice the most important NumPy copy and view questions with complete solutions. NumPy Copy and View practice questions with solutions help to understand the concepts.
1. Python Program to Create a Copy of a NumPy Array
Problem Statement
Write a Python program to create a copy of a NumPy array using the copy() method.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40])
new_array = numbers.copy()
print(new_array)
Sample Output
[10 20 30 40]
Explanation
The copy() method creates a completely new array with its own memory location. Changes made to the copied array do not affect the original array.
Concepts Covered
copy()- Deep Copy
- Independent Memory
2. Python Program to Modify a Copied Array
Problem Statement
Write a Python program to modify a copied array and verify that the original array remains unchanged.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
copied = numbers.copy()
copied[0] = 100
print("Original:", numbers)
print("Copy:", copied)
Sample Output
Original: [10 20 30]
Copy: [100 20 30]
Explanation
Since copy() creates a new array, modifying the copied array does not change the original array.
Concepts Covered
- Copy Method
- Array Modification
3. Python Program to Create a View of a NumPy Array
Problem Statement
Write a Python program to create a view of a NumPy array using the view() method.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
view_array = numbers.view()
print(view_array)
Sample Output
[10 20 30]
Explanation
The view() method creates another reference to the same data instead of creating a new copy.
Concepts Covered
view()- Shared Memory
4. Python Program to Modify a View
Problem Statement
Write a Python program to modify a view and observe the changes in the original array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
view_array = numbers.view()
view_array[1] = 200
print("Original:", numbers)
print("View:", view_array)
Sample Output
Original: [ 10 200 30]
View: [ 10 200 30]
Explanation
A view shares memory with the original array. Any changes made to the view are reflected in the original array.
Concepts Covered
- Shared Memory
- View Method
5. Python Program to Check the Base of a Copied Array
Problem Statement
Write a Python program to check the base attribute of a copied array.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3])
copied = numbers.copy()
print(copied.base)
Sample Output
None
Explanation
The base attribute returns None because a copied array owns its own memory.
Concepts Covered
base- Copy Ownership
6. Python Program to Check the Base of a View
Problem Statement
Write a Python program to check the base attribute of a view.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3])
view_array = numbers.view()
print(view_array.base)
Sample Output
[1 2 3]
Explanation
The base attribute points to the original array because a view shares the same memory.
Concepts Covered
- Base Array
- Shared Reference
7. Python Program to Compare Copy and View
Problem Statement
Write a Python program to compare the behavior of copy() and view().
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
copied = numbers.copy()
view_array = numbers.view()
numbers[0] = 999
print("Original:", numbers)
print("Copy:", copied)
print("View:", view_array)
Sample Output
Original: [999 20 30]
Copy: [10 20 30]
View: [999 20 30]
Explanation
The copied array remains unchanged, while the view reflects the changes made to the original array.
Concepts Covered
- Copy vs View
- Memory Sharing
8. Python Program to Check Whether Two Arrays Share Memory
Problem Statement
Write a Python program to determine whether two arrays share the same memory.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
view_array = numbers.view()
print(view_array.base is numbers)
Sample Output
True
Explanation
The expression returns True because the view references the original array.
Concepts Covered
- Memory Sharing
- Identity Check
9. Python Program to Create Multiple Views
Problem Statement
Write a Python program to create two views of the same NumPy array.
Python Solution
import numpy as np
numbers = np.array([5, 10, 15])
view1 = numbers.view()
view2 = numbers.view()
print(view1)
print(view2)
Sample Output
[ 5 10 15]
[ 5 10 15]
Explanation
Multiple views can reference the same original array without creating additional copies.
Concepts Covered
- Multiple Views
- Shared Data
10. Python Program to Verify Independent Memory Using Copy
Problem Statement
Write a Python program to verify that a copied array uses independent memory.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
copied = numbers.copy()
numbers[2] = 500
print("Original:", numbers)
print("Copy:", copied)
Sample Output
Original: [ 10 20 500]
Copy: [10 20 30]
Explanation
The copied array is stored in separate memory, so changes to the original array do not affect the copied array.
Concepts Covered
- Independent Memory
- Deep Copy
11. Python Program to Demonstrate Difference Between NumPy Copy and View
Problem Statement
Write a Python program to create a NumPy array and demonstrate the difference between copy() and view() by modifying the original array.
Python Solution
import numpy as np
# Original array
array = np.array([10, 20, 30, 40, 50])
# Create copy and view
copy_array = array.copy()
view_array = array.view()
# Modify original array
array[0] = 100
print("Original Array:")
print(array)
print("\nCopy Array:")
print(copy_array)
print("\nView Array:")
print(view_array)
Sample Output
Original Array:
[100 20 30 40 50]
Copy Array:
[10 20 30 40 50]
View Array:
[100 20 30 40 50]
Explanation
copy()creates a completely independent array. Changes in the original array do not affect the copy.view()creates a new array that shares the same memory. Changes in the original array affect the view.
Concepts Covered
- NumPy Copy
- NumPy View
- Memory Sharing
- Array Modification
12. Python Program to Modify a View and Observe Changes in Original Array
Problem Statement
Write a Python program to create a NumPy array, create its view, modify the view, and check whether the original array changes.
Python Solution
import numpy as np
array = np.array([5, 15, 25, 35, 45])
# Create view
view_array = array.view()
print("Original Array Before Modification:")
print(array)
# Modify view
view_array[2] = 500
print("\nView After Modification:")
print(view_array)
print("\nOriginal Array After View Modification:")
print(array)
Sample Output
Original Array Before Modification:
[ 5 15 25 35 45]
View After Modification:
[ 5 15 500 35 45]
Original Array After View Modification:
[ 5 15 500 35 45]
Explanation
A view does not create a separate memory location. Both arrays point to the same underlying data, so changes in one array appear in the other.
Concepts Covered
- View Modification
- Shared Memory
- Array References
13. Python Program to Check Whether Two NumPy Arrays Share Memory
Problem Statement
Write a Python program to check whether a NumPy array and its view share the same memory location.
Python Solution
import numpy as np
array = np.array([1, 2, 3, 4, 5])
copy_array = array.copy()
view_array = array.view()
print("Copy Shares Memory:")
print(np.shares_memory(array, copy_array))
print("\nView Shares Memory:")
print(np.shares_memory(array, view_array))
Sample Output
Copy Shares Memory:
False
View Shares Memory:
True
Explanation
The np.shares_memory() function checks whether two arrays use the same memory block.
- Copy → Separate memory
- View → Same memory
Concepts Covered
- shares_memory()
- Memory Management
- Copy vs View
14. Python Program to Create a View from a Sliced NumPy Array
Problem Statement
Write a Python program to create a sliced array, convert it into a view, modify the view, and observe the changes in the original array.
Python Solution
import numpy as np
array = np.array([10, 20, 30, 40, 50, 60])
# Create sliced view
slice_array = array[1:5]
print("Original Array:")
print(array)
print("\nSliced Array:")
print(slice_array)
# Modify sliced array
slice_array[0] = 200
print("\nModified Sliced Array:")
print(slice_array)
print("\nOriginal Array After Modification:")
print(array)
Sample Output
Original Array:
[10 20 30 40 50 60]
Sliced Array:
[20 30 40 50]
Modified Sliced Array:
[200 30 40 50]
Original Array After Modification:
[ 10 200 30 40 50 60]
Explanation
NumPy slicing usually creates a view instead of a copy. Therefore, modifying the sliced array also changes the original array.
Concepts Covered
- Array Slicing
- Views
- Memory Sharing
- Data Modification
15. Python Program to Compare Performance of Copy and View Operations
Problem Statement
Write a Python program to compare the memory usage of a NumPy copy and view using a large array.
Python Solution
import numpy as np
# Create large array
array = np.arange(1000000)
# Create copy and view
copy_array = array.copy()
view_array = array.view()
print("Original Array Memory:")
print(array.nbytes, "bytes")
print("\nCopy Array Memory:")
print(copy_array.nbytes, "bytes")
print("\nView Array Memory:")
print(view_array.nbytes, "bytes")
Sample Output
Original Array Memory:
8000000 bytes
Copy Array Memory:
8000000 bytes
View Array Memory:
8000000 bytes
Explanation
Although copy() and view() may show the same size using nbytes, the difference is in memory allocation:
- Copy → Allocates new memory space.
- View → Uses the same memory as the original array.
Views are faster and memory-efficient for large datasets.
Concepts Covered
- Memory Efficiency
- nbytes
- Copy vs View
- Large Array Handling
Chapter Summary
In this chapter, you learned the difference between copy() and view() in NumPy. You practiced creating copied arrays, creating views, checking shared memory with the base attribute, and understanding how changes affect original and referenced arrays.
Key Takeaways
copy()creates a completely independent array.view()creates another reference to the same data.- Changes in a copied array do not affect the original array.
- Changes in a view are reflected in the original array.
- The
baseattribute helps identify memory sharing. - Copies use separate memory, while views share memory.
- Understanding copy and view is important for writing memory-efficient NumPy programs.
Frequently Asked Questions (FAQs)
1. What is the difference between copy() and view() in NumPy?
copy() creates a new array with its own memory, while view() shares the memory of the original array.
2. Does modifying a copied array affect the original array?
No. A copied array is independent, so changes do not affect the original array.
3. Does modifying a view affect the original array?
Yes. Since a view shares memory with the original array, changes are reflected in both.
4. What is the purpose of the base attribute?
The base attribute helps determine whether an array owns its memory or shares memory with another array.
5. When should I use copy()?
Use copy() when you need an independent array that won’t be affected by changes to the original.
6. When should I use view()?
Use view() when you want to save memory and work with the same data without creating another copy.
7. Why is understanding copy and view important?
It helps you avoid unexpected data changes and write efficient NumPy programs for data analysis and machine learning.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

