w3resource

NumPy: Copy data from a given array to another array

NumPy: Array Object Exercise-148 with Solution

Write a NumPy program to copy data from a given array to another array.

Sample Solution:

Python Code:

import numpy as np
x = np.array([24, 27, 30, 29, 18, 14])
print("Original array:")
print(x)
y = np.empty_like (x)
y[:] = x
print("\nCopy of the said array:")
print(y)

Sample Output:

Original array:
[24 27 30 29 18 14]

Copy of the said array:
[24 27 30 29 18 14]

Pictorial Presentation:

NumPy: Copy data from a given array to another array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create an array that represents the rank of each item of a given array.
Next: Write a NumPy program to find elements within range from a given array of numbers.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]