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

NumPy · Function

np.expand_dims

Adds one or more singleton dimensions to an array at specified axis positions. The result is an ndarray view, and a tuple of axes can insert multiple dimensions.

Since
1.18.0

How it works

np.expand_dims returns a view of the input with one additional dimension when axis is an integer, or additional dimensions when axis is a tuple. The inserted dimensions have length one, and axis positions determine where they appear in the resulting shape.

Syntax

numpy.expand_dims(a, axis)

Parameters

NameTypeDescription
aarray_likeInput array.
axisint or tuple of intsPosition in the expanded axes where the new axis or axes is placed.

Returns

res — ndarray: A view of a with the number of dimensions increased by one for a single axis; tuple axes add one dimension per axis.

Key points

  • The function increases an array's rank by inserting singleton dimensions.
  • An integer axis controls the position of the inserted dimension in the output shape.
  • axis accepts an integer or tuple of integers describing where one or more new axes go.
  • For a single axis, the result is a view with one more dimension than the input.
  • Passing a tuple lets one call add multiple singleton dimensions.
  • For a trailing singleton dimension, expand_dims can replace equivalent np.newaxis indexing.

Example: Add row and column dimensions

import numpy as np

x = np.array([1, 2])
rows = np.expand_dims(x, axis=0)
columns = np.expand_dims(x, axis=1)

print(rows.shape)     # (1, 2)
print(columns.shape)  # (2, 1)

Adds a leading or trailing singleton dimension to make a one-dimensional array row- or column-shaped.

When to use it

Create a row vector

Convert a flat vector into a row-shaped array for operations that expect a leading batch or row dimension.

Create a column vector

Convert a flat vector into a column-shaped array for matrix multiplication or broadcasting.

Prepare broadcast-compatible shapes

Insert several singleton dimensions at once when aligning shapes for broadcasting.

Interview questions

QWhat does np.expand_dims do, and what does it return?

It inserts singleton dimensions at the requested axis positions and returns a view of the input. With an integer axis, the rank increases by one; a tuple can insert several dimensions.

Follow-up: How would you create a column vector from a one-dimensional array? Use np.expand_dims(x, axis=1), which is equivalent to x[:, np.newaxis].

QHow does the axis argument affect the result shape?

axis specifies where the new dimension is placed in the expanded shape. For a one-dimensional array, axis=0 produces shape (1, n), while axis=1 produces (n, 1).

Follow-up: Can axis be repeated in a tuple? No; each axis must be unique, otherwise NumPy raises a ValueError.

QCan np.expand_dims add more than one dimension?

The function accepts an array-like input and an integer or tuple of integers for axis. A tuple inserts one new dimension for each supplied axis, allowing multiple dimensions to be added in one call.

Follow-up: What alternatives express a single-axis expansion? Indexing with np.newaxis or None can express equivalent reshaping operations.

  • numpy.newaxis
  • numpy.squeeze
  • numpy.reshape
  • numpy.atleast_1d
  • numpy.atleast_2d
  • numpy.atleast_3d

Source: numpy.expand_dims — NumPy v2.3 Manual. Checked 2026-10-15.