Open In App
Related Articles

How to Copy NumPy array into another array?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. In this

Copy NumPy Array into Another Array

There are various ways to copies created in NumPy arrays in Python, here we are discussing some generally used methods for copies created in NumPy arrays those are following.

Copy 1D Numpy Arrays into Another Using np.copy() Function

In the below example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function.

Python3

# importing Numpy package
import numpy as np
 
# Creating a numpy array using np.array()
org_array = np.array([1.54, 2.99, 3.42, 4.87, 6.94,
                      8.21, 7.65, 10.50, 77.5])
 
print("Original array: ")
 
# printing the Numpy array
print(org_array)
 
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
 
print("\nCopied array: ")
 
# printing the copied Numpy array
print(copy_array)

                    

Output:

Original array: 
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]
Copied array:
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]

Copy Given 2-D Array to Another Array Using np.copy() Function

In this example, the given 2-D Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function.

Python3

# importing Numpy package
import numpy as np
 
# Creating a 2-D numpy array using np.array()
org_array = np.array([[23, 46, 85],
                      [43, 56, 99],
                      [11, 34, 55]])
 
print("Original array: ")
 
# printing the Numpy array
print(org_array)
 
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
 
print("\nCopied array: ")
 
# printing the copied Numpy array
print(copy_array)

                    

Output:

Original array: 
[[23 46 85]
[43 56 99]
[11 34 55]]
Copied array:
[[23 46 85]
[43 56 99]
[11 34 55]]

NumPy Array Copy Using Assignment Operator

In the below example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using Assignment Operator.

Python3

# importing Numpy package
import numpy as np
 
# Create a 2-D Numpy array using np.array()
org_array = np.array([[99, 22, 33],
                      [44, 77, 66]])
 
# Copying org_array to copy_array
# using Assignment operator
copy_array = org_array
 
# modifying org_array
org_array[1, 2] = 13
 
# checking if copy_array has remained the same
 
# printing original array
print('Original Array: \n', org_array)
 
# printing copied array
print('\nCopied Array: \n', copy_array)

                    

Output:

Original Array: 
[[99 22 33]
[44 77 13]]
Copied Array:
[[99 22 33]
[44 77 13]]


Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads