Python

# Copy Array in Python

NumPy is a very useful library of Python to create different types of arrays and perform numerical computations. The array is a data structure of Python to store multiple elements by NumPy. The array can be copied in python by using the assignment operator, different built-in functions of the NumPy library, and for a loop. Different ways of copying an array in Python have been shown in this tutorial.

## Example 1: Copy an Array Using the ‘=’ Operator

An array can be copied to another array by using the “=” assignment operator. But this operator does not create a duplicate copy of the main array and just creates a reference of the original array. So, if any value is changed to the original array then it will change the value of the copied array also. Create a Python file with the following script to check the use of the assignment operator for copying an array.

#Import the NumPy module
import numpy as np

#Create an array
array1 = np.array([34.5, 89.34, 23.6, 45.72, 62.87])

#Copy the array using '=' operator
array2 = array1

#Modify the 4th element of the main array
array1[3] = 10.12

print("The output after modifying the main array")
#Display both arrays
print("The content of the main array", array1)
print("The content of the copied array", array2)

#Modify the 1st element of the copied array
array2[0] = 20.34

print("\nThe output after modifying the copied array")
#Display both arrays again
print("The content of the main array", array1)
print("The content of the copied array", array2)

Output
The following output will appear after executing the above script. The output shows that the changes in the main array change in the copied array and the changes in copied array change in the main array.

## Example 2: Copy an Array Using copy() Function

Using the copy() function is another way of copying an array in Python. In this case, a new array object is created from the original array and this type of copy is called deep copy. If any value is modified in the original or copied array, then it does not create any change on another array. The syntax of the copy() function is given below.

Syntax

array2 = array1.copy()

The copy() method doesn’t take any argument and it returns a new array with the values of the original array.

Create a Python file with the following script that will copy a NumPy array by using the copy() function. Here, the value of the original array and the copied array has been changed in the script.

#Import NumPy module
import numpy as np

#Create an array
array1 = np.array([67, 45, 78, 12, 56, 51])

#Copy the array using copy() function
array2 = array1.copy()

#Modify the  3rd element of the main array
array1[2] = 99

print("The output after modifying the main array")
#Display both arrays
print("The content of the main array", array1)
print("The content of the copied array", array2)

#Modify the  6th element of the copied array
array2[5] = 0

print("\nThe output after modifying the copied array")
#Display both arrays again
print("The content of the main array", array1)
print("The content of the copied array", array2)

Output
The following output will appear after executing the above script. The output shows that changing in the main array does not change the value of the copied array and the changing in the copied array does not change the value of the main array.

## Example 3: Copy an Array Using view() Function

Using the view() function is another way of copying an array in Python. But this function does not create a duplicate copy of the main array and just creates a reference of the original array. So, if any value is changed to the original array then it will change the value of the copied array also. This type of copy is called shallow copy. The syntax of the view() function is given below.

Syntax

array2 = array1.view()

Create a Python file with the following script that will copy a NumPy array by using the view() function. Here, the value of the original array and the copied array has been changed in the script.

#Import NumPy module
import numpy as np

#Create an array
array1 = np.array([7, 4, 7, 2, 1, 9])

#Copy the array using view() function
array2 = array1.view()

#Modify the 1st element of the main array
array1[0] = 11

print("The output after modifying the main array")
#Display both arrays
print("The content of the main array", array1)
print("The content of the copied array", array2)

#Modify the  5th element of the copied array
array2[4] = 99

print("\nThe output after modifying the copied array")
#Display both arrays again
print("The content of the main array", array1)
print("The content of the copied array", array2)

Output
The following output will appear after executing the above script. The output shows that changing in the main array changes the values of the copied array and changing in the copied array changes the value of the main array.

## Example 4: Copy Array Using Loop

Create a Python file with the following script to know the way of copying an array without using any built-in function or assignment operator. Any loop can be used to copy the values of an array to another array. The ‘for’ loop has been used in the script to copy an array to another array.

#Import NumPy module
import numpy as np

#Create an array
array1 = np.array([11, 22, 33, 44, 55, 66, 77, 88])

#Create the second array with the size of first array
array2 = [None]*len(array1)

#Copying all elements of one array into another
for i in range(0, len(array1)):
array2[i] = array1[i];

#Modify the 2nd element of the main array
array1[1] = 25

#Modify the 4th element of the copied array
array2[3] = 45

print("The output after modifying the first and second arrays")
#Display both arrays
print("The content of the original array", array1)
print("The content of the copied array", array2)

Output
The following output will appear after executing the above script. The output shows that changing in the main array does not change the value of the copied array and the changing in the copied array does not change the value of the main array.

## Conclusion

The ways of copying arrays by using assignment operator, built-in functions, and loop have been shown in this tutorial. The assignment operator and view() function create a shallow copy of the original array and the copy() function and loop create a deep copy of the original array.