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

NumPy · Function

np.swapaxes

Interchanges two axes of an array. It is useful for changing dimension order while leaving the data values unchanged.

Since
1.10.0

How it works

swapaxes returns an array whose two selected dimensions exchange positions while all other axes remain in their original order. For an ndarray, the result is a view; for other inputs, NumPy creates a new array.

Syntax

numpy.swapaxes(a, axis1, axis2)

Parameters

NameTypeDescription
aarray_likeInput array or array-like object whose axes will be exchanged.
axis1intFirst axis to interchange.
axis2intSecond axis to interchange.

Returns

a_swapped ndarray. For NumPy >= 1.10.0, an ndarray input produces a view; otherwise a new array is created.

Key points

  • Use this function when two dimensions need to exchange positions without specifying a full permutation.
  • axis1 is the integer index of the first dimension to exchange.
  • axis2 is the integer index of the second dimension to exchange.
  • The function returns an ndarray representing the input with the selected axes exchanged.

Common mistakes

  • The result usually shares storage with an ndarray input, so changes through the result can affect the original array.

Example: Swap two axes of a 3-D array

import numpy as np

x = np.array([[[0, 1], [2, 3]],
              [[4, 5], [6, 7]]])
y = np.swapaxes(x, 0, 2)

print(y.shape)  # (2, 2, 2)
print(y[:, :, 0])

This exchanges axes 0 and 2, changing how the same elements are indexed.

When to use it

Prepare multidimensional data

Reordering dimensions before an operation that expects a particular axis layout.

Adapt tensor layouts

Changing channel, batch, or spatial dimension placement in tensor-like arrays.

Create a view with a new axis order

Avoiding an unnecessary data copy when working with ndarray inputs.

Interview questions

QWhat does numpy.swapaxes do?

It exchanges the positions of two axes and returns the resulting array. The two axes are selected with axis1 and axis2; dimensions not selected retain their relative order.

Follow-up: How does it differ from transpose? transpose can specify a complete axis permutation, while swapaxes performs the focused operation of exchanging two axes.

QWhat do the axis arguments mean?

axis1 identifies the first axis and axis2 identifies the second axis. The function returns an ndarray with those axes interchanged.

Follow-up: What happens if the same axis is supplied twice? The axis order is unchanged, so the result has the same shape and indexing arrangement as the input.

QDoes swapaxes copy its input?

For an ndarray input, NumPy returns a view rather than copying the data. If the input is not an ndarray, NumPy creates a new array.

Follow-up: Why can this matter? Mutating a writable result may mutate the original ndarray because both can refer to the same underlying data.

  • numpy.transpose
  • numpy.moveaxis
  • numpy.rollaxis
  • numpy.ndarray.swapaxes

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