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)













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


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, March 28, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250