How to Check If Copy Changes When Original Array Changes


In this example, we will learn how to create a copy and check if the copy changes when the original NumPy array changes in Python.

Source Code

import numpy as np

n = np.array([1, 2, 3, 4])
copy_n = n.copy()
print(copy_n)

n[2] = 10

# any changes made to the original array will not affect the copy
print(n)
print(copy_n)

Output:

[1 2 3 4]
[ 1  2 10  4]
[1 2 3 4]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement