It's easier to understand what np.vstack, np.hstack and np.dstack* do by looking at the .shape attribute of the output array.

Using your two example arrays:

print(a.shape, b.shape)
# (3, 2) (3, 2)
  • np.vstack concatenates along the first dimension...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    
  • np.hstack concatenates along the second dimension...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    
  • and np.dstack concatenates along the third dimension.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

Since a and b are both two dimensional, np.dstack expands them by inserting a third dimension of size 1. This is equivalent to indexing them in the third dimension with np.newaxis (or alternatively, None) like this:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

If c = np.dstack((a, b)), then c[:, :, 0] == a and c[:, :, 1] == b.

You could do the same operation more explicitly using np.concatenate like this:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)

* Importing the entire contents of a module into your global namespace using import * is considered bad practice for several reasons. The idiomatic way is to import numpy as np.

Answer from ali_m on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-numpy-dstack-method
Numpy dstack() method-Python - GeeksforGeeks
June 12, 2025 - numpy.dstack() stacks arrays depth-wise along the third axis (axis=2). For 1D arrays, it promotes them to (1, N, 1) before stacking. For 2D arrays, it stacks them along axis=2 to form a 3D array.
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.4 Manual
>>> import numpy as np >>> a = np.array((1,2,3)) >>> b = np.array((4,5,6)) >>> np.dstack((a,b)) array([[[1, 4], [2, 5], [3, 6]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[4],[5],[6]]) >>> np.dstack((a,b)) array([[[1, 4]], [[2, 5]], [[3, 6]]]) Go BackOpen In Tab ·
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.3 Manual
>>> import numpy as np >>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.dstack((a,b)) array([[[1, 2], [2, 3], [3, 4]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[2],[3],[4]]) >>> np.dstack((a,b)) array([[[1, 2]], [[2, 3]], [[3, 4]]]) Go BackOpen In Tab ·
Top answer
1 of 4
91

It's easier to understand what np.vstack, np.hstack and np.dstack* do by looking at the .shape attribute of the output array.

Using your two example arrays:

print(a.shape, b.shape)
# (3, 2) (3, 2)
  • np.vstack concatenates along the first dimension...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    
  • np.hstack concatenates along the second dimension...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    
  • and np.dstack concatenates along the third dimension.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

Since a and b are both two dimensional, np.dstack expands them by inserting a third dimension of size 1. This is equivalent to indexing them in the third dimension with np.newaxis (or alternatively, None) like this:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

If c = np.dstack((a, b)), then c[:, :, 0] == a and c[:, :, 1] == b.

You could do the same operation more explicitly using np.concatenate like this:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)

* Importing the entire contents of a module into your global namespace using import * is considered bad practice for several reasons. The idiomatic way is to import numpy as np.

2 of 4
6

Let x == dstack([a, b]). Then x[:, :, 0] is identical to a, and x[:, :, 1] is identical to b. In general, when dstacking 2D arrays, dstack produces an output such that output[:, :, n] is identical to the nth input array.

If we stack 3D arrays rather than 2D:

x = numpy.zeros([2, 2, 3])
y = numpy.ones([2, 2, 4])
z = numpy.dstack([x, y])

then z[:, :, :3] would be identical to x, and z[:, :, 3:7] would be identical to y.

As you can see, we have to take slices along the third axis to recover the inputs to dstack. That's why dstack behaves the way it does.

🌐
MindSpore
mindspore.cn › mindspore.numpy.dstack
mindspore.numpy.dstack | MindSpore 1.6 documentation | MindSpore
>>> import mindspore.numpy as np >>> x1 = np.array([1, 2, 3]).astype('float32') >>> x2 = np.array([4, 5, 6]).astype('float32') >>> output = np.dstack((x1, x2)) >>> print(output) [[[1. 4.] [2. 5.] [3.
🌐
Programiz
programiz.com › python-programming › numpy › methods › dstack
NumPy dstack()
The dstack() method stacks the sequence of input arrays depthwise. import numpy as np array1 = np.array([[0, 1], [2, 3]]) array2 = np.array([[4, 5], [6, 7]])
🌐
w3resource
w3resource.com › numpy › manipulation › dstack.php
NumPy: numpy.dstack() function - w3resource
The numpy.dstack() is used to stack arrays in sequence depth wise (along third axis). This is equivalent to concatenation along the third axis after 2-D arrays of shape (M,N) have been reshaped to (M,N,1) and 1-D arrays of shape (N,) have been ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_dstack_function.htm
Numpy dstack() Function
The Numpy dstack() function is used to stack arrays in sequence depth-wise (along the third axis). This function is part of the numpy module. It is useful for stacking multiple arrays to create a 3D array, where each input array becomes a layer in
🌐
Pydocs
pydocs.github.io › p › numpy › 1.22.4 › api › numpy.dstack.html
Document
>>> a = np.array((1,2,3)) ... b = np.array((2,3,4)) ... np.dstack((a,b)) array([[[1, 2], [2, 3], [3, 4]]]) >>> a = np.array([[1],[2],[3]]) ... b = np.array([[2],[3],[4]]) ... np.dstack((a,b)) array([[[1, 2]], [[2, 3]], [[3, 4]]]) See : The following pages refer to to this document either explicitly or contain code examples using this.
🌐
Medium
medium.com › @andiksyldnata › understanding-numpy-dstack-in-python-eb40e4467c09
Understanding NumPy dstack in Python | by 99spaceidea | Medium
June 21, 2023 - The dstack() function takes a sequence of arrays as input and returns a single array that is stacked along the third axis. The arrays in the sequence must have the same shape along all but the third axis.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.dstack((a,b)) array([[[1, 2], [2, 3], [3, 4]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[2],[3],[4]]) >>> np.dstack((a,b)) array([[[1, 2]], [[2, 3]], [[3, 4]]]) On this page
🌐
TikTok
tiktok.com › @idek_who_u_are › photo › 7622369505607568653
Beginner's Guide to Trading in Adopt Me
TikTok - trends start here. On a device or on the web, viewers can watch and discover millions of personalized short videos. Download the app to get started.
🌐
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.stack.html
jax.numpy.stack — JAX documentation
arrays (np.ndarray | Array | Sequence[ArrayLike]) – a sequence of arrays to stack; each must have the same shape.
🌐
Towards Data Science
towardsdatascience.com › home › data science › np.stack() - how to stack two arrays in numpy and python
np.stack() - How To Stack two Arrays in Numpy And Python | Towards Data Science
January 10, 2023 - The np concatenate function takes elements of all input arrays and returns them as a single 1D array. The numpy dstack function allows you to combine arrays index by index and store the results like a stack.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.stack.html
numpy.stack — NumPy v2.3 Manual
>>> import numpy as np >>> rng = np.random.default_rng() >>> arrays = [rng.normal(size=(3,4)) for _ in range(10)] >>> np.stack(arrays, axis=0).shape (10, 3, 4)
🌐
Educative
educative.io › answers › what-is-the-numpydstack-function-in-numpy
What is the numpy.dstack() function in NumPy?
The dstack() function in NumPy is used to stack or arrange the given arrays in a sequence depth wise (that is, along the third axis), thereby creating an array of at least 3-D.
🌐
Spec-zone
spec-zone.ru › numpy~1.14 › generated › numpy.dstack
numpy.dstack()
Для работы Spec-Zone.ru требуется JavaScript, включите в настройках вашего браузера
🌐
Esri Community
community.esri.com › t5 › python-questions › stack-2d-array-using-numpy-dstack-memory-error › td-p › 664223
Solved: stack 2D array using numpy dstack memory error - Esri Community
December 12, 2021 - import arcpy from arcpy.sa import * import os, sys import numpy as np from scipy.signal import savgol_filter arcpy.CheckOutExtension("Spatial") rasters = [] ws = 'G:/Test/Raster' for folder, subs, files in os.walk(ws): for filename in files: aSrc = arcpy.RasterToNumPyArray(os.path.join(folder,filename)) rasters.append(aSrc) stack = np.dstack((rasters[0], rasters[1], rasters[2], rasters[3], rasters[4], rasters[5],rasters[6], rasters[7], rasters[8], rasters[9]))
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> a = np.array((1,2,3)) >>> b = np.array((4,5,6)) >>> np.dstack((a,b)) array([[[1, 4], [2, 5], [3, 6]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[4],[5],[6]]) >>> np.dstack((a,b)) array([[[1, 4]], [[2, 5]], [[3, 6]]]) Go BackOpen In Tab ·
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › dstack()
Python Numpy dstack() - Stack Arrays Depthwise
November 18, 2024 - The numpy.dstack() function in Python offers a powerful approach to stacking arrays depth-wise along the third dimension. It works effectively for combining images, data frames, or any set of matrices where a third-dimensional aggregation is desired.