You are close, you want to use np.tile, but like this:

a = np.array([0,1,2])
np.tile(a,(3,1))

Result:

array([[0, 1, 2],
   [0, 1, 2],
   [0, 1, 2]])

If you call np.tile(a,3) you will get concatenate behavior like you were seeing

array([0, 1, 2, 0, 1, 2, 0, 1, 2])

http://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html

Answer from Cory Kramer on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.repeat.html
numpy.repeat โ€” NumPy v2.4 Manual
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]])
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-repeat
How To Repeat Arrays N Times In Python NumPy?
May 16, 2025 - This example demonstrates how repeating arrays can help in creating realistic simulations by duplicating known patterns and then adding variations. ... When dealing with large arrays, performance becomes important. Letโ€™s compare the methods: import numpy as np import time # Create a medium-sized array original = np.arange(1000) # Test np.repeat start = time.time() np.repeat(original, 100) print(f"np.repeat: {time.time() - start:.6f} seconds") # Test np.tile start = time.time() np.tile(original, 100) print(f"np.tile: {time.time() - start:.6f} seconds") # Test list multiplication start = time.time() np.array(original.tolist() * 100) print(f"List multiplication: {time.time() - start:.6f} seconds") # Test concatenate start = time.time() np.concatenate([original] * 100) print(f"np.concatenate: {time.time() - start:.6f} seconds")
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ numpy โ€บ repeat
Python Numpy repeat() - Repeat Elements | Vultr Docs
November 14, 2024 - The numpy.repeat() function in Python is a versatile tool used in data manipulation and transformation, particularly within the NumPy library. This function repeats elements of an array a specified number of times, creating a larger array according ...
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.

๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-25.php
NumPy: Construct an array by repeating - w3resource
August 29, 2025 - Python Code: # Importing the NumPy library with an alias 'np' import numpy as np # Creating an original array a = [1, 2, 3, 4] print("Original array") print(a) # Repeating the original array two times using np.tile print("Repeating 2 times") x = np.tile(a, 2) print(x) # Repeating the original array three times using np.tile print("Repeating 3 times") x = np.tile(a, 3) print(x) Sample Output: Original array [1, 2, 3, 4] Repeating 2 times [1 2 3 4 1 2 3 4] Repeating 3 times [1 2 3 4 1 2 3 4 1 2 3 4] Explanation: In the above code - a = [1, 2, 3, 4]: A list a is defined with four elements.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-numpy-duplicate-copy-array
Python Numpy - Duplicate or Copy Array Data to Another Array
Python Program to Copy Numpy Array - To copy array data to another using Python Numpy, you can use numpy.ndarray.copy() function as follows: array2 = array1.copy() where array1 is a numpy n-dimensional array. array1.copy() returns a new array but with the exact element values as that of array1.
Find elsewhere
๐ŸŒ
Predictivehacks
predictivehacks.com
How to repeat each list element n times in Python โ€“ Predictive Hacks
Letโ€™s say that we have the following list [1,2,3,4,5] and we want to repeat each element 3 times. Letโ€™s see how we can do it in Python and numpy.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.repeat.html
numpy.repeat โ€” NumPy v2.3 Manual
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]])
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.tile.html
numpy.tile โ€” NumPy v2.4 Manual
Repeat elements of an array. ... Try it in your browser! >>> import numpy as np >>> a = np.array([0, 1, 2]) >>> np.tile(a, 2) array([0, 1, 2, 0, 1, 2]) >>> np.tile(a, (2, 2)) array([[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]]) >>> np.tile(a, (2, 1, 2)) array([[[0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2]]])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.repeat.html
numpy.repeat โ€” NumPy v2.5.dev0 Manual
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]])
Top answer
1 of 2
9

Use the timeit module in python for testing timings.

from copy import *

a=range(1000)

def cop():
    b=copy(a)

def func1():
    b=list(a)

def slice():
    b=a[:]

def slice_len():
    b=a[0:len(a)]



if __name__=="__main__":
    import timeit
    print "copy(a)",timeit.timeit("cop()", setup="from __main__ import cop")
    print "list(a)",timeit.timeit("func1()", setup="from __main__ import func1")
    print "a[:]",timeit.timeit("slice()", setup="from __main__ import slice")
    print "a[0:len(a)]",timeit.timeit("slice_len()", setup="from __main__ import slice_len")

Results:

copy(a) 3.98940896988
list(a) 2.54542589188
a[:] 1.96630120277                   #winner
a[0:len(a)] 10.5431251526

It's surely the extra steps involved in a[0:len(a)] are the reason for it's slowness.

Here's the byte code comparison of the two:

In [19]: dis.dis(func1)
  2           0 LOAD_GLOBAL              0 (range)
              3 LOAD_CONST               1 (100000)
              6 CALL_FUNCTION            1
              9 STORE_FAST               0 (a)

  3          12 LOAD_FAST                0 (a)
             15 SLICE+0             
             16 STORE_FAST               1 (b)
             19 LOAD_CONST               0 (None)
             22 RETURN_VALUE        

In [20]: dis.dis(func2)
  2           0 LOAD_GLOBAL              0 (range)
              3 LOAD_CONST               1 (100000)
              6 CALL_FUNCTION            1
              9 STORE_FAST               0 (a)

  3          12 LOAD_FAST                0 (a)    #same up to here
             15 LOAD_CONST               2 (0)    #loads 0
             18 LOAD_GLOBAL              1 (len) # loads the builtin len(),
                                                 # so it might take some lookup time
             21 LOAD_FAST                0 (a)
             24 CALL_FUNCTION            1         
             27 SLICE+3             
             28 STORE_FAST               1 (b)
             31 LOAD_CONST               0 (None)
             34 RETURN_VALUE        
2 of 2
6

I can't comment on the ruby timing vs. the python timing. But I can comment on list vs. slice. Here's a quick inspection of the bytecode:

>>> import dis
>>> a = range(10)
>>> def func(a):
...     return a[:]
... 
>>> def func2(a):
...     return list(a)
... 
>>> dis.dis(func)
  2           0 LOAD_FAST                0 (a)
              3 SLICE+0             
              4 RETURN_VALUE        
>>> dis.dis(func2)
  2           0 LOAD_GLOBAL              0 (list)
              3 LOAD_FAST                0 (a)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE 

Notice that list requires a LOAD_GLOBAL to find the function list. Looking up globals (and calling functions) in python is relatively slow. This would explain why a[0:len(a)] is also slower. Also remember that list needs to be able to handle arbitrary iterators whereas slicing doesn't. This means that list needs to allocate a new list, pack elements into that list as it iterates over the list and resize when necessary. There are a few things in here which are expensive -- resizing if necessary and iterating (effectively in python, not C). With the slicing method, you can calculate the size of the memory you'll need so can probably avoid resizing, and the iteration can be done completely in C (probably with a memcpy or something.

disclaimer : I'm not a python dev, so I don't know how the internals of list() are implemented for sure. I'm just speculating based what I know of the specification.

EDIT -- So I've looked at the source (with a little guidance from Martijn). The relevant code is in listobject.c. list calls list_init which then calls listextend at line 799. That function has some checks to see if it can use a fast branch if the object is a list or a tuple (line 812). Finally, the heavy lifting is done starting at line 834:

 src = PySequence_Fast_ITEMS(b);
 dest = self->ob_item + m;
 for (i = 0; i < n; i++) {
     PyObject *o = src[i];
     Py_INCREF(o);
     dest[i] = o;
 }

Compare that to the slice version which I think is defined in list_subscript (line 2544). That calls list_slice (line 2570) where the heavy lifting is done by the following loop (line 486):

 src = a->ob_item + ilow;
 dest = np->ob_item;
 for (i = 0; i < len; i++) {
     PyObject *v = src[i];
     Py_INCREF(v);
     dest[i] = v;
 }

They're pretty much the same code, so it's not surprising that the performance is almost the same for large lists (where the overhead of the small stuff like unpacking slices, looking up global variables, etc becomes less important)


Here's how I would run the python tests (and the results for my Ubuntu system):

$ python -m timeit -s 'a=range(30)' 'list(a)'
1000000 loops, best of 3: 0.39 usec per loop
$ python -m timeit -s 'a=range(30)' 'a[:]'
10000000 loops, best of 3: 0.183 usec per loop
$ python -m timeit -s 'a=range(30)' 'a[0:len(a)]'
1000000 loops, best of 3: 0.254 usec per loop
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-26.php
NumPy: Repeat elements of an array - w3resource
August 29, 2025 - The resulting 1D array is [1, 1, 2, 2, 3, 3, 4, 4]. The final output is printed: [1, 1, 2, 2, 3, 3, 4, 4]. ... Use np.repeat to duplicate each element of an array a specified number of times and check the new length.