Python Program to Copy an Array

Write a Python Program to copy the Numpy Array to another. In Python, we can use the equals operator to copy the complete Numpy Array to another.

# Array Copy
import numpy as np

cparr = np.array([10, 20, 30, 40, 50])

copyarr = cparr

print("***Numpy Arrya Copy Result***")
print("Original Array = ", cparr)
print("Copied Array   = ", copyarr)

Python Numpy Array copy output

***Numpy Arrya Copy Result***
Original Array =  [10 20 30 40 50]
Copied Array   =  [10 20 30 40 50]

In this Python example, we used the Tuple slicing technique to copy the required items to another Numpy Array.

# Array Copy

import numpy as np

cparr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

copyarr1 = cparr
copyarr2 = cparr[2:7]
copyarr3 = cparr[3:]
copyarr4 = cparr[::-1]

print("***Numpy Arrya Index Copy Result by Slicing***")
print("Original Array = ", cparr)
print("Copied Array   = ", copyarr1)
print("Copy Array from 2 to 7   = ", copyarr2)
print("Copy Array from 3 to End   = ", copyarr3)
print("Reverse Array   = ", copyarr4)
Python Program to Copy an Array 2

Python Program to Copy an Array using the For Loop range.

# Array Copy
import numpy as np

cparr = np.array([12, 22, 35, 55, 47])
copyarr = np.empty(5)

for i in range(len(cparr)):
    copyarr[i] = cparr[i]

print("***Numpy Arrya Copy Result***")
print("Original Array = ", cparr)
print("Copied Array   = ", copyarr)

Copy Python Numpy Array using for loop output

***Numpy Arrya Copy Result***
Original Array =  [12 22 35 55 47]
Copied Array   =  [12. 22. 35. 55. 47.]