w3resource logo
Python Exercises

Python NumPy: Repeat elements of an array

Python NumPy: Array Object Exercise-26 with Solution

Write a Python program to repeat elements of an array.

Sample Solution:-

Python Code:

import numpy as np
x = np.repeat(3, 4)
print(x)
x = np.array([[1,2],[3,4]])
print(np.repeat(x, 2))

Sample Output:

[3 3 3 3]                                                              
[1 1 2 2 3 3 4 4]

Python Code Editor:

import numpy as np
x = np.repeat(3, 4)
print(x)
x = np.array([[1,2],[3,4]])
print(np.repeat(x, 2))

Improve this sample solution and post your code through Disqus