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

NumPy · Function

np.squeeze

Removes axes of length one from an array. Use axis to restrict removal to specific singleton dimensions.

Since
1.7.0

How it works

np.squeeze returns an array with axes whose length is one removed. With axis=None, every singleton axis is removed; with an integer or tuple, only the selected axes are considered, and each selected axis must have length one.

Syntax

numpy.squeeze(a, axis=None)

Parameters

NameTypeDescription
aarray_likeInput data.
axisNone or int or tuple of ints, optionalSelects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised.

Returns

squeezed — ndarray. The input array with all or a subset of dimensions of length 1 removed; it is always a itself or a view into a.

Key points

  • The function eliminates dimensions whose size equals one.
  • An explicit axis argument limits the operation to chosen singleton dimensions.
  • The returned ndarray preserves the data while changing its dimensional shape.
  • The result is either the original array object or a view into the input, rather than an independent copied array.
  • Squeezing axis 0 from shape (1, 3, 1) leaves shape (3, 1).

Common mistakes

  • Selecting an axis whose size is not one raises ValueError; squeeze does not remove non-singleton axes.
  • Removing every axis can produce a zero-dimensional ndarray rather than a Python scalar.

Example: Remove singleton axes

import numpy as np

x = np.array([[[0], [1], [2]]])
print(x.shape)                  # (1, 3, 1)
print(np.squeeze(x).shape)      # (3,)
print(np.squeeze(x, axis=0).shape)  # (3, 1)

Use no axis to remove every length-one axis, or provide an axis to remove only selected length-one axes.

When to use it

Normalize array shapes

Normalize inputs from APIs or batches that add singleton dimensions before downstream numerical operations.

Targeted dimension removal

Remove only a known singleton batch or channel dimension while preserving other dimensions.

Collapse singleton arrays

Convert a shape such as (1, 1) to a 0-dimensional ndarray when scalar-like array behavior is required.

Interview questions

QWhat does np.squeeze do, and how does axis affect it?

It removes axes of length one from an array. By default, every such axis is removed; an integer or tuple restricts removal to selected axes.

Follow-up: What happens when a selected axis has size greater than one? NumPy raises ValueError because that axis cannot be squeezed.

QWhat does np.squeeze return when all dimensions are removed?

The input may be array-like, and the result is an ndarray with all or the selected length-one dimensions removed. If no axes remain, the result is a 0-dimensional array.

Follow-up: How would you obtain the contained value from a 0-dimensional result? Index it with [()] as shown in the documentation example.

QWhat values can axis take?

axis may be None, an integer, or a tuple of integers. None targets all singleton dimensions, while an integer or tuple selects a subset; selected dimensions must have size one.

Follow-up: Why might specifying axis be safer than leaving it as None? It prevents unrelated singleton dimensions from being removed.

  • numpy.expand_dims
  • numpy.reshape

Source: numpy.squeeze. Checked 2026-10-15.