By my tests, np.repeat is a little faster than np.tile:

X = np.repeat(arr[None,:], 3, axis=0)

Alternatively, use np.concatenate:

X = np.concatenate([[arr]] * 3, axis=0)

arr = np.arange(10000 * 1000).reshape(10000, 1000)

%timeit np.repeat(arr[None,:], 3, axis=0)
%timeit np.tile(arr, (3, 1, 1))
%timeit np.concatenate([[arr]] * 3, axis=0)
# Read-only, array cannot be modified.
%timeit np.broadcast_to(arr, (3, *arr.shape))
# Creating copy of the above.
%timeit np.broadcast_to(arr, (3, *arr.shape)).copy()

170 ms ± 3.82 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
187 ms ± 3.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
243 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
10.9 µs ± 218 ns per loop (mean ± std. dev. of 7 runs, 100000 loops 
189 ms ± 2.45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)each) 

np.array_equals(np.repeat(arr[None,:], 3, axis=0), 
                np.tile(arr, (3, 1, 1))
True
Answer from coldspeed95 on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.repeat.html
numpy.repeat — NumPy v2.6.dev0 Manual
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array. ... Output array which has the same shape as a, except along the given axis. ... Tile an array. ... Find the unique elements of an array. ... Try it in your browser! >>> import numpy as np >>> np.repeat(3, 4) array([3, 3, 3, 3]) >>> np.repeat([4, 5, 6], [1, 2, 3]) array([4, 5, 5, 6, 6, 6]) >>> x = np.array([[1,2],[3,4]]) >>> np.repeat(x, 2) array([1, 1, 2, 2, 3, 3, 4, 4]) >>> np.repeat(x, 3, axis=1) array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]) >>> np.repeat(x, [1, 2], axis=0) array([[1, 2], [3, 4], [3, 4]])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.repeat.html
numpy.repeat — NumPy v2.5 Manual
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array. ... Output array which has the same shape as a, except along the given axis. ... Tile an array. ... Find the unique elements of an array. ... Try it in your browser! >>> import numpy as np >>> np.repeat(3, 4) array([3, 3, 3, 3]) >>> x = np.array([[1,2],[3,4]]) >>> np.repeat(x, 2) array([1, 1, 2, 2, 3, 3, 4, 4]) >>> np.repeat(x, 3, axis=1) array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]) >>> np.repeat(x, [1, 2], axis=0) array([[1, 2], [3, 4], [3, 4]])
🌐
Programiz
programiz.com › python-programming › numpy › methods › repeat
NumPy repeat() | Programiz
August 30, 2023 - The repeat() method repeats the elements of the input arrays a given number of times. import numpy as np numbers = np.array([0, 1, 2, 3])
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .repeat()
Python:NumPy | ndarray | .repeat() | Codecademy
November 4, 2025 - The .repeat() method of a NumPy ndarray returns a new array where each element is repeated a specified number of times.
Top answer
1 of 3
7

There are a number of options in numpy:

  1. The simplest is np.stack, with the new axis set to 0:

    result = np.stack((arr,) * m, axis=0)
    

    This is a generalization of np.vstack, which can accept a variable m in the same way:

    result = np.vstack((arr,) * m)
    
  2. Since you are looking to expand the first dimension and not the second, you can use np.tile:

    result = np.tile(arr, [m, 1])
    
  3. For this simple case, you can repeat the elements individually for the same result using np.repeat. This is a bit hacky, but it works just as well:

    result = np.repeat([arr], m, axis=0)
    

    If arr is already a numpy array, you can add the leading dimension more explicitly:

    result = np.repeat(arr[None, :], m, axis=0)
    
  4. A slightly more convoluted method, but which showcases what is going on under the hood better is using np.concatenate:

    np.concatenate(([arr],) * m, axis=0)
    
  5. In the same spirit as concatenate, you can use the constructor np.array directly:

    np.array([arr] * m)
    
  6. An ugly method is to use muliplication (combined with broadcasting):

    result = arr * np.ones((m, 1), dtype=np.int8)
    

    Setting the dtype to a very small integer type pretty much ensures that the result will be promoted to the actual type of your array.

  7. The previous methods copy the array multiple times to get the 2D data. But since the data is repeated, you don't actually need to copy it. Instead, you can make a view that creates a fake first dimension using broadcasting. Specifically, the np.broadcast_to function will do exactly what you want:

     result = np.broadcast_to(arr, [m, len(arr)])
    
  8. There is a more general approach even than the previous to making views is np.lib.stride_tricks.as_strided.

    First, let's reiterate the warning from the documentation:

    Warning

    This function has to be used with extreme care, see notes.

    Unless you know the byte-size of the dtype you plan on using ahead of time, it's a good idea to ensure that arr is a numpy array to get the strides from it:

    arr = np.asanyarray(arr)
    result = np.lib.stride_tricks.as_strided(arr, shape=(m, arr.size), strides=(0,) + arr.strides)
    

In the last two cases, if you want to expand the view into a full buffer that contains a proper copy for each element, you can copy the data:

result = result.copy()

If you don't do the expansion, you will effectively be simulating the behavior of multiple references to the same list, where modifying any element changes all elements in the same column.

2 of 3
1

You can use:

import numpy as np
arr, m = [1,2,3,4], 3

np.vstack([arr]*m)

Output:

array([[1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4]])

On another note, why do you need to create such a matrix? You may be looking for numpy broadcasting without the need of repeating your array.

🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.repeat.html
numpy.repeat — NumPy v2.3 Manual
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array. ... Output array which has the same shape as a, except along the given axis. ... Tile an array. ... Find the unique elements of an array. ... Try it in your browser! >>> import numpy as np >>> np.repeat(3, 4) array([3, 3, 3, 3]) >>> x = np.array([[1,2],[3,4]]) >>> np.repeat(x, 2) array([1, 1, 2, 2, 3, 3, 4, 4]) >>> np.repeat(x, 3, axis=1) array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]) >>> np.repeat(x, [1, 2], axis=0) array([[1, 2], [3, 4], [3, 4]])
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-repeat-python
numpy.repeat() in Python - GeeksforGeeks
March 28, 2022 - # Python Program illustrating # numpy.repeat() import numpy as geek #Working on 1D arr = geek.arange(5) print("arr : \n", arr) repetitions = 2 a = geek.repeat(arr, repetitions) print("\nRepeating arr 2 times : \n", a) print("Shape : ", a.shape) repetitions = 3 a = geek.repeat(arr, repetitions) print("\nRepeating arr 3 times : \n", a) # [0 0 0 ..., 4 4 4] means [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4] # since it was long output, so it uses [ ...
Find elsewhere
Top answer
1 of 3
20

One can use np.repeat methods together with np.newaxis:

import numpy as np

test = np.random.randn(40,40,3)
result = np.repeat(test[np.newaxis,...], 10, axis=0)
print(result.shape)
>> (10, 40, 40, 3)
2 of 3
10

Assuming you're looking to copy the values 10 times, you can just stack 10 of the array:

def repeat(arr, count):
    return np.stack([arr for _ in range(count)], axis=0)

axis=0 is actually the default, so it's not really necessary here, but I think it makes it clearer that you're adding the new axis on the front.


In fact, this is pretty much identical to what the examples for stack are doing:

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)

At first glance you might think repeat or tile would be a better fit.

But repeat is about repeating over an existing axis (or flattening the array), so you'd need to reshape either before or after. (Which is just as efficient, but I think not as simple.)

And tile (assuming you use an array-like reps—with scalar reps it basically repeat) is about filling out a multidimensional spec in all directions, which is much more complex than what you want for this simple case.


All of these options will be equally efficient. They all copy the data 10 times over, which is the expensive part; the cost of any internal processing, building tiny intermediate objects, etc. is irrelevant. The only way to make it faster is to avoid copying. Which you probably don't want to do.

But if you do… To share row storage across the 10 copies, you probably want broadcast_to:

def repeat(arr, count):
    return np.broadcast_to(arr, (count,)+arr.shape)

Notice that broadcast_to doesn't actually guarantee that it avoids copying, just that it returns some kind of readonly view where "more than one element of a broadcasted array may refer to a single memory location". In practice, it's going to avoid copying. If you actually need that to be guaranteed for some reason (or if you want a writable view—which is usually going to be a terrible idea, but maybe you have a good reason…), you have to drop down to as_strided:

def repeat(arr, count):
    shape = (count,) + arr.shape
    strides = (0,) + arr.strides
    return np.lib.stride_tricks.as_strided(
        arr, shape=shape, strides=strides, writeable=False)

Notice that half the docs for as_strided are warning that you probably shouldn't use it, and the other half are warning that you definitely shouldn't use it for writable views, so… make sure this is what you want before doing it.

🌐
Python Guides
pythonguides.com › python-numpy-repeat
NumPy repeat(): Elements, Rows, Columns and vs tile
September 27, 2026 - np.repeat duplicates each element in place while np.tile repeats the whole array. How the axis argument works, and why leaving it out flattens your array.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › repeat()
Python Numpy repeat() - Repeat Elements | Vultr Docs
November 14, 2024 - Define a condition for how many times each element should be repeated based on the element's value or index. Apply numpy.repeat() with varying repetitions. ... repetitions = [2, 3, 4] dynamic_repeated_array = np.repeat(a, repetitions) ...
🌐
w3resource
w3resource.com › numpy › manipulation › repeat.php
NumPy: numpy.repeat() function - w3resource
April 24, 2026 - The numpy.repeat() function is primarily used to repeat elements of an array, creating a new array with repeated values based on the specified repetition count.
🌐
Codegive
codegive.com › blog › numpy_repeat_array_n_times.php
Numpy repeat array n times
a: The input array. This can be any ndarray or array-like object (e.g., a Python list or tuple that NumPy can convert to an array). repeats: This is the crucial parameter specifying how many times each element (or slice along an axis) should be repeated.
🌐
Medium
giulio-laurenti.medium.com › repeating-the-elements-of-numpy-arrays-9da737f578c9
Repeating the Elements of Numpy Arrays | by Giulio Laurenti, PhD | Medium
June 30, 2025 - First, import Numpy. ... repeats: integer or array of integers. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. axis (optional): the axis along which to repeat values. By default, use the flattened input array, and return a flat output array. # Create an array np.random.seed(34) arr = np.random.randint(20, 50, 4) arr array([21, 46, 30, 29]) # Repeat each element of arr three times rep = np.repeat(arr, 3) rep array([21, 21, 21, 46, 46, 46, 30, 30, 30, 29, 29, 29])
🌐
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .repeat()
Python:NumPy | Built-in Functions | .repeat() | Codecademy
April 8, 2025 - The .repeat() function in NumPy is used to duplicate items in an array. It provides the option to specify the number of times each element appears, and whether that repetition happens across a specific axis or not.
Top answer
1 of 2
69
In [1]: data = np.arange(-50,50,10)

To repeat each element 5 times use np.repeat:

In [3]: np.repeat(data, 5)
Out[3]: 
array([-50, -50, -50, -50, -50, -40, -40, -40, -40, -40, -30, -30, -30,
       -30, -30, -20, -20, -20, -20, -20, -10, -10, -10, -10, -10,   0,
         0,   0,   0,   0,  10,  10,  10,  10,  10,  20,  20,  20,  20,
        20,  30,  30,  30,  30,  30,  40,  40,  40,  40,  40])

To repeat the array 5 times use np.tile:

In [2]: np.tile(data, 5)
Out[2]: 
array([-50, -40, -30, -20, -10,   0,  10,  20,  30,  40, -50, -40, -30,
       -20, -10,   0,  10,  20,  30,  40, -50, -40, -30, -20, -10,   0,
        10,  20,  30,  40, -50, -40, -30, -20, -10,   0,  10,  20,  30,
        40, -50, -40, -30, -20, -10,   0,  10,  20,  30,  40])

Note, however, that sometimes you can take advantage of NumPy broadcasting instead of creating a larger array with repeated elements.

For example, if

z = np.array([1, 2])
v = np.array([[3], [4], [5]])

then to add these arrays to produce

 [[4 5]
  [5 6]
  [6 7]]

you do not need to use tile:

In [12]: np.tile(z, (3,1))
Out[12]: 
array([[1, 2],
       [1, 2],
       [1, 2]])

In [13]: np.tile(v, (1,2))
Out[13]: 
array([[3, 3],
       [4, 4],
       [5, 5]])

In [14]: np.tile(z, (3,1)) + np.tile(v, (1,2))
Out[14]: 
array([[4, 5],
       [5, 6],
       [6, 7]])

Instead, NumPy will broadcast the arrays for you:

In [15]: z + v
Out[15]: 
array([[4, 5],
       [5, 6],
       [6, 7]])
2 of 2
7

Simply use np.repeat:

In [5]: data.repeat(5)

Out[5]: 
array([-50, -50, -50, -50, -50, -40, -40, -40, -40, -40, -30, -30, -30,
       -30, -30, -20, -20, -20, -20, -20, -10, -10, -10, -10, -10,   0,
         0,   0,   0,   0,  10,  10,  10,  10,  10,  20,  20,  20,  20,
        20,  30,  30,  30,  30,  30,  40,  40,  40,  40,  40])