Probably the cleanest way is to use np.repeat:

a = np.array([[1, 2], [1, 2]])
print(a.shape)
# (2,  2)

# indexing with np.newaxis inserts a new 3rd dimension, which we then repeat the
# array along, (you can achieve the same effect by indexing with None, see below)
b = np.repeat(a[:, :, np.newaxis], 3, axis=2)

print(b.shape)
# (2, 2, 3)

print(b[:, :, 0])
# [[1 2]
#  [1 2]]

print(b[:, :, 1])
# [[1 2]
#  [1 2]]

print(b[:, :, 2])
# [[1 2]
#  [1 2]]

Having said that, you can often avoid repeating your arrays altogether by using broadcasting. For example, let's say I wanted to add a (3,) vector:

c = np.array([1, 2, 3])

to a. I could copy the contents of a 3 times in the third dimension, then copy the contents of c twice in both the first and second dimensions, so that both of my arrays were (2, 2, 3), then compute their sum. However, it's much simpler and quicker to do this:

d = a[..., None] + c[None, None, :]

Here, a[..., None] has shape (2, 2, 1) and c[None, None, :] has shape (1, 1, 3)*. When I compute the sum, the result gets 'broadcast' out along the dimensions of size 1, giving me a result of shape (2, 2, 3):

print(d.shape)
# (2,  2, 3)

print(d[..., 0])    # a + c[0]
# [[2 3]
#  [2 3]]

print(d[..., 1])    # a + c[1]
# [[3 4]
#  [3 4]]

print(d[..., 2])    # a + c[2]
# [[4 5]
#  [4 5]]

Broadcasting is a very powerful technique because it avoids the additional overhead involved in creating repeated copies of your input arrays in memory.


* Although I included them for clarity, the None indices into c aren't actually necessary - you could also do a[..., None] + c, i.e. broadcast a (2, 2, 1) array against a (3,) array. This is because if one of the arrays has fewer dimensions than the other then only the trailing dimensions of the two arrays need to be compatible. To give a more complicated example:

a = np.ones((6, 1, 4, 3, 1))  # 6 x 1 x 4 x 3 x 1
b = np.ones((5, 1, 3, 2))     #     5 x 1 x 3 x 2
result = a + b                # 6 x 5 x 4 x 3 x 2
Answer from ali_m on Stack Overflow
Top answer
1 of 7
273

Probably the cleanest way is to use np.repeat:

a = np.array([[1, 2], [1, 2]])
print(a.shape)
# (2,  2)

# indexing with np.newaxis inserts a new 3rd dimension, which we then repeat the
# array along, (you can achieve the same effect by indexing with None, see below)
b = np.repeat(a[:, :, np.newaxis], 3, axis=2)

print(b.shape)
# (2, 2, 3)

print(b[:, :, 0])
# [[1 2]
#  [1 2]]

print(b[:, :, 1])
# [[1 2]
#  [1 2]]

print(b[:, :, 2])
# [[1 2]
#  [1 2]]

Having said that, you can often avoid repeating your arrays altogether by using broadcasting. For example, let's say I wanted to add a (3,) vector:

c = np.array([1, 2, 3])

to a. I could copy the contents of a 3 times in the third dimension, then copy the contents of c twice in both the first and second dimensions, so that both of my arrays were (2, 2, 3), then compute their sum. However, it's much simpler and quicker to do this:

d = a[..., None] + c[None, None, :]

Here, a[..., None] has shape (2, 2, 1) and c[None, None, :] has shape (1, 1, 3)*. When I compute the sum, the result gets 'broadcast' out along the dimensions of size 1, giving me a result of shape (2, 2, 3):

print(d.shape)
# (2,  2, 3)

print(d[..., 0])    # a + c[0]
# [[2 3]
#  [2 3]]

print(d[..., 1])    # a + c[1]
# [[3 4]
#  [3 4]]

print(d[..., 2])    # a + c[2]
# [[4 5]
#  [4 5]]

Broadcasting is a very powerful technique because it avoids the additional overhead involved in creating repeated copies of your input arrays in memory.


* Although I included them for clarity, the None indices into c aren't actually necessary - you could also do a[..., None] + c, i.e. broadcast a (2, 2, 1) array against a (3,) array. This is because if one of the arrays has fewer dimensions than the other then only the trailing dimensions of the two arrays need to be compatible. To give a more complicated example:

a = np.ones((6, 1, 4, 3, 1))  # 6 x 1 x 4 x 3 x 1
b = np.ones((5, 1, 3, 2))     #     5 x 1 x 3 x 2
result = a + b                # 6 x 5 x 4 x 3 x 2
2 of 7
48

Another way is to use numpy.dstack. Supposing that you want to repeat the matrix a num_repeats times:

import numpy as np
b = np.dstack([a]*num_repeats)

The trick is to wrap the matrix a into a list of a single element, then using the * operator to duplicate the elements in this list num_repeats times.

For example, if:

a = np.array([[1, 2], [1, 2]])
num_repeats = 5

This repeats the array of [1 2; 1 2] 5 times in the third dimension. To verify (in IPython):

In [110]: import numpy as np

In [111]: num_repeats = 5

In [112]: a = np.array([[1, 2], [1, 2]])

In [113]: b = np.dstack([a]*num_repeats)

In [114]: b[:,:,0]
Out[114]: 
array([[1, 2],
       [1, 2]])

In [115]: b[:,:,1]
Out[115]: 
array([[1, 2],
       [1, 2]])

In [116]: b[:,:,2]
Out[116]: 
array([[1, 2],
       [1, 2]])

In [117]: b[:,:,3]
Out[117]: 
array([[1, 2],
       [1, 2]])

In [118]: b[:,:,4]
Out[118]: 
array([[1, 2],
       [1, 2]])

In [119]: b.shape
Out[119]: (2, 2, 5)

At the end we can see that the shape of the matrix is 2 x 2, with 5 slices in the third dimension.

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.tile.html
numpy.tile — NumPy v2.4 Manual
>>> b = np.array([[1, 2], [3, 4]]) >>> np.tile(b, 2) array([[1, 2, 1, 2], [3, 4, 3, 4]]) >>> np.tile(b, (2, 1)) array([[1, 2], [3, 4], [1, 2], [3, 4]])
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.repeat.html
numpy.repeat — NumPy v2.3 Manual
Input array. ... The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. ... The axis along which to repeat values.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.stack.html
numpy.stack — NumPy v2.4 Manual
The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension. ... Each array must have the same shape. In the case of a single ndarray array_like input, it will be treated as a sequence of arrays; i.e., each element along ...
🌐
w3resource
w3resource.com › numpy › manipulation › repeat.php
NumPy: numpy.repeat() function - w3resource
May 30, 2024 - The function is useful in data ... same shape as a, except along the specified axis. ... In the above code, the numpy.repeat() function ......
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.repeat.html
numpy.repeat — NumPy v2.5.dev0 Manual
Input array. ... The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. ... The axis along which to repeat values.
🌐
Llego
llego.dev › home › blog › numpy: tiling and repeating arrays
NumPy: Tiling and Repeating Arrays - llego.dev
March 11, 2023 - Learn techniques to efficiently tile and repeat NumPy arrays for data analysis and modeling. Code examples using numpy.split, numpy.repeat, numpy.tile, and more.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › repeat
Python Numpy repeat() - Repeat Elements | Vultr Docs
November 14, 2024 - # Creating a two-dimensional array ... This distinction is crucial for manipulating arrays in data preparation. The numpy.repeat() function is a powerful method for efficiently duplicating array elements in Python....
🌐
GeeksforGeeks
geeksforgeeks.org › python › insert-a-new-axis-within-a-numpy-array
Insert a new axis within a NumPy array - GeeksforGeeks
July 15, 2025 - In this case, the output is (3, 1, 4), indicating an additional dimension added along axis 1. ... In this method uses NumPy's `expand_dims` method to insert multiple new axes into a given array simultaneously.
Find elsewhere
🌐
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.repeat.html
jax.numpy.repeat — JAX documentation
>>> repeats = jnp.array([2, 3]) >>> jnp.repeat(a, repeats, axis=1) Array([[1, 1, 2, 2, 2], [3, 3, 4, 4, 4]], dtype=int32)
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.tile.html
numpy.tile — NumPy v2.5.dev0 Manual
Construct an array by repeating A the number of times given by reps. If reps has length d, the result will have dimension of max(d, A.ndim). If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication.
🌐
NumPy
numpy.org › doc › 2.2 › reference › routines.array-manipulation.html
Array manipulation routines — NumPy v2.2 Manual
copyto(dst, src[, casting, where]) · Copies values from one array to another, broadcasting as necessary
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.02-the-basics-of-numpy-arrays.html
The Basics of NumPy Arrays | Python Data Science Handbook
This can be done with the reshape method, or more easily done by making use of the newaxis keyword within a slice operation: ... We will see this type of transformation often throughout the remainder of the book. All of the preceding routines worked on single arrays.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-56.php
NumPy: Insert a new axis within a 2-D array - w3resource
August 29, 2025 - y = np.expand_dims(x, axis=1).shape: The np.expand_dims() function takes the input array x and adds a new axis along the specified axis, in this case, axis=1. The original shape of x is (3, 4), and after expanding the dimensions, the new shape ...
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_join.asp
NumPy Joining Array
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr = np.concatenate((arr1, arr2), axis=1) print(arr) Try it Yourself » · Stacking is same as concatenation, the only difference is that stacking is done along a new axis.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.vstack.html
numpy.vstack — NumPy v2.1 Manual
Split an array into a tuple of sub-arrays along an axis. ... >>> import numpy as np >>> a = np.array([1, 2, 3]) >>> b = np.array([4, 5, 6]) >>> np.vstack((a,b)) array([[1, 2, 3], [4, 5, 6]])
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.4 Manual
You can explicitly convert a 1D array to either a row vector or a column vector using np.newaxis. For example, you can convert a 1D array to a row vector by inserting an axis along the first dimension:
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matrix.copy.html
numpy.matrix.copy — NumPy v2.4 Manual
>>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)