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
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.

🌐
w3resource
w3resource.com › numpy › manipulation › dstack.php
NumPy: numpy.dstack() function - w3resource
NumPy Array manipulation: numpy.dstack() function, example - The numpy.dstack() is used to stack arrays in sequence depth wise (along third axis).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-numpy-dstack-method
Numpy dstack() method-Python - GeeksforGeeks
June 12, 2025 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 12 Jun, 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.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.5 Manual
numpy.dstack(tup)[source]# 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 reshaped to (1,N,1).
🌐
GeeksforGeeks
geeksforgeeks.org › python-numpy-dstack-method
Python | Numpy dstack() method | GeeksforGeeks
September 19, 2019 - Python | Numpy dstack() method · With the help of numpy.dstack() method, we can get the combined array index by index and store like a stack by using numpy.dstack() method. Syntax : numpy.dstack((array1, array2)) Return : Return combined array ...
🌐
IncludeHelp
includehelp.com › python › what-is-the-numpy-dstack-function-in-numpy.aspx
Python - What is the numpy.dstack() function in NumPy?
December 21, 2023 - The numpy.dstack() function returns an array formed by stacking the given arrays, the returned array will be at least a 3D array.
Find elsewhere
🌐
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.
🌐
Dstack
dstack.ai › docs › reference › api › python
Python API - dstack
The Python API enables running tasks, services, and managing runs programmatically. Below is a quick example of submitting a task for running and displaying its logs. import sys from dstack.api import Task, GPU, Client, Resources client = ...
🌐
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.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.6.dev0 Manual
numpy.dstack(tup)[source]# 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 reshaped to (1,N,1).
🌐
GitHub
github.com › pushpen › dstack
GitHub - pushpen/dstack: An open-source tool for building data and ML applications using Python and R · GitHub
Uploading datasets and visualization to the server is done via the dstack packages available for both Python and R.
Author: pushpen
🌐
GitHub
github.com › sha-shrestha › dstack
GitHub - sha-shrestha/dstack: An open-source tool to rapidly develop data applications with Python · GitHub
dstack is an open-source Python library that makes it easy to build and manage web applications for data science and machine learning.
Author: sha-shrestha
🌐
PyPI
pypi.org › project › dstack
dstack · PyPI
Python :: 3 · Topic · Scientific/Engineering :: Artificial Intelligence · Report project as malware · Download the file for your platform. If you're not sure which to choose, learn more about installing packages. dstack-0.21.2.tar.gz (36.1 MB view details) Uploaded Aug 19, 2026 Source ·
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_dstack_function.htm
Numpy dstack() Function
Python TechnologiesDatabasesComputer ... View All Categories ... The Numpy dstack() function is used to stack arrays in sequence depth-wise (along the third axis)....
🌐
GitHub
github.com › dstackai › dstack
GitHub - dstackai/dstack: A unified orchestration layer for heterogeneous AI compute. It standardizes how to manage compute and run training and inference on GPU clouds, Kubernetes, VMs, or bare-metal clusters. · GitHub
A unified orchestration layer for heterogeneous AI compute. It standardizes how to manage compute and run training and inference on GPU clouds, Kubernetes, VMs, or bare-metal clusters. - dstackai/dstack
Author: dstackai
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ma.dstack.html
numpy.ma.dstack — NumPy v2.3 Manual
ma.dstack = <numpy.ma.extras._fromnxfunction_seq object># 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 reshaped to (1,N,1).