Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.stack

Stacks a sequence of equally shaped arrays along a new axis. Use axis to choose where that dimension appears, and optionally provide an output buffer, dtype, and casting policy.

Since
1.10.0

How it works

np.stack converts a sequence of equally shaped arrays into one array by inserting a new dimension at the position selected by axis. With axis=0 the new dimension leads the result; with axis=-1 it trails it. The optional out buffer receives the result, while dtype and casting control conversion.

Syntax

numpy.stack(arrays, axis=0, out=None, *, dtype=None, casting='same_kind')

Parameters

NameTypeDescription
arrayssequence of array_likeSequence of array-like inputs to stack; each input must have the same shape.
axisint, optionalPosition of the new axis in the result. The default is 0.
outndarray, optionalOptional destination array for the result.
dtypedtype, optionalOptional data type for the result.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalControls permitted data casting.

Returns

An ndarray containing the input arrays stacked along the newly inserted axis.

Key points

  • The function combines multiple arrays while adding one dimension rather than joining along an existing dimension.
  • axis=0 inserts the new dimension before all existing dimensions.
  • axis=-1 inserts the new dimension after all existing dimensions.
  • Input arrays must be shape-compatible in the strict sense that their complete shapes match.
  • An out array can receive the result when supplied.

Common mistakes

  • All input arrays need identical shapes; stack does not combine differently shaped inputs.
  • axis chooses where the extra dimension is inserted, so changing it changes the output shape and layout.

Example: Stack vectors along different axes

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

rows = np.stack((a, b))
columns = np.stack((a, b), axis=-1)

print(rows)
print(columns)

Creates one two-dimensional array by inserting a leading axis, then another by inserting the new axis at the end.

When to use it

Create batches

Representing multiple same-shaped observations as one batch dimension.

Control tensor layout

Choosing whether the sample dimension leads or trails existing dimensions.

Reuse an output buffer

Writing results into caller-managed storage.

Interview questions

QWhat does np.stack do?

np.stack joins a sequence of arrays along a newly created axis. If the inputs have shape (M, N), stacking K of them produces a result with K elements in the inserted dimension.

Follow-up: How does axis=-1 differ from the default? It places the new dimension at the end rather than at the beginning.

QHow does axis affect the output shape?

The default axis is 0, so the new dimension becomes the first dimension of the result. For ten inputs of shape (3, 4), the result therefore has shape (10, 3, 4).

Follow-up: What shape results from axis=1 for those inputs? The shape is (3, 10, 4).

QWhat constraints and controls apply to the arguments?

arrays is a sequence of array-like values, and every member must have the same shape. out may provide the destination array; dtype selects the result type and casting controls permitted conversions.

Follow-up: When would out be useful? It lets callers place the computed result into a preallocated destination instead of relying only on a newly returned allocation.

  • numpy.concatenate
  • numpy.stack
  • numpy.block
  • numpy.vstack
  • numpy.hstack
  • numpy.dstack

Source: numpy.stack — NumPy v2.5 Manual. Checked 2026-10-15.