Python Program to Copy an Array

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

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)

Output

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

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

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.

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)

using for loop output

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