Python repeat array n times [using 5 methods]

Do you want to repeat an array n times in Python? In this Python tutorial, I will show you five methods related to “Python repeat array n times” with some illustrative examples.

To repeat an array n times in Python, you can use the * operator with lists (e.g., repeated_array = original_array * n), utilize numpy.tile() for numpy arrays, or employ list comprehension (e.g., [element for element in original_array for _ in range(n)]).

Python repeat array n times

Now, let me show you five different methods to repeat an array n times in Python:

  • Using the * Operator
  • NumPy’s repeat() Function
  • Using List Comprehension
  • The tile() Function in NumPy
  • The itertools Module

Let’s see them one by one using some illustrative examples.

1. Python repeat array n times using the * operator

One of the simplest ways to repeat an array in Python is using the multiplication (*) operator. This method is straightforward and works well with lists, so we will use the list constructor to convert the array to a list.

Here, is an example with code:

import numpy as np

original_array = np.array(["NY", "CA", "TX"])
n = 2
repeated_array = np.array(list(original_array) * n)
print(repeated_array)

Output:

['NY' 'CA' 'TX' 'NY' 'CA' 'TX']

After the implementation of the code in the Python Pycharm editor, we have mentioned the screenshot below:

repeat numpy array n times

2. NumPy repeat array n times using repeat() function

One of the best ways to repeat elements of an array in Python is by using NumPy’s repeat function. This function repeats each element of the array n times in Python.

READ:  How to Create a Snake game in Python using Turtle

Here is the complete code, of how Python repeat array n times:

import numpy as np

original_array = np.array([75, 68, 60])
n = 3
repeated_array = np.repeat(original_array, n)
print(repeated_array)

Output:

[75 75 75 68 68 68 60 60 60]

After executing the code in Pycharm, one can see the output in the below screenshot.

python repeat array n times

3. Python repeat the array n times using list comprehension

List comprehension in Python offers a more Pythonic way to repeat arrays, as it helps us to create a list with conditions. After the list is created, we use the array function to create an array.

import numpy as np

original_array = np.array([21.43, 3.14, 1.89])
n = 2
repeated_array = np.array([element for element in original_array for _ in range(n)])
print(repeated_array)

Output:

[21.43 21.43  3.14  3.14  1.89  1.89]

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below:

numpy copy array n times in Python

4. NumPy repeat array n times in Python using the tile() function

The tile() function in NumPy repeats the entire array in Python. It’s highly efficient for multi-dimensional arrays.

import numpy as np

original_array = np.array(["Statue of Liberty", "Grand Canyon", "Mount Rushmore"])
n = 2
repeated_array = np.tile(original_array, n)
print(repeated_array)

Output:

['Statue of Liberty' 'Grand Canyon' 'Mount Rushmore' 'Statue of Liberty'
 'Grand Canyon' 'Mount Rushmore']

After the implementation, the screenshot is mentioned below:

repeat an array n times python

5. repeat NumPy array n times using the itertools module

Python’s itertools module provides a function called chain.from_iterable, which can be used for repeating arrays.

from itertools import chain
import numpy as np

original_array = np.array([8398748, 3990456, 2705994])
n = 2
repeated_array = np.array(list(chain.from_iterable([original_array] * n)))
print(repeated_array)

Output:

[8398748 3990456 2705994 8398748 3990456 2705994]

After executing the code in Pycharm, one can see the output in the below screenshot.

python duplicate array n times

Conclusion

We have learned five different methods related to “Python repeat array n times” like the * operator, repeat() function, list comprehension, tile() function, and itertools module with some examples.

READ:  How NumPy Filter 2D Array by Condition in Python [6 Methods]

The choice of method depends on the requirement of the problem.

You may also like to read: