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

NumPy · Function

np.transpose

Reorders the axes of an array. Without axes it reverses their order; with a permutation, it applies the specified output-axis order.

How it works

np.transpose returns an array whose axes are reordered. With no axes argument it reverses every axis; with an explicit permutation, each output axis takes its data from the corresponding input axis.

Syntax

numpy.transpose(a, axes=None)

Parameters

NameTypeDescription
aarray_likeInput array-like object whose axes will be reordered.
axestuple or list of ints, optionalOptional tuple or list of integers specifying the output-axis permutation.

Returns

An ndarray with the axes of a permuted.

Key points

  • The function produces an array with its dimensions reordered.
  • On a matrix, np.transpose swaps rows and columns.
  • For higher-dimensional arrays, provide an axis-order sequence to choose the output layout.
  • Omitting axes reverses all dimensions, rather than only swapping the first two.
  • Axis permutations may use negative indices as an alternative to nonnegative axis numbers.

Common mistakes

  • An explicit axes argument must contain every axis exactly once; it cannot omit, duplicate, or invent axis numbers.
  • Transposing a one-dimensional array does not change its shape or orientation.

Example: Permute selected axes

import numpy as np

x = np.arange(24).reshape(2, 3, 4)
y = np.transpose(x, (1, 0, 2))

print(x.shape)  # (2, 3, 4)
print(y.shape)  # (3, 2, 4)
print(y[1, 0, 2])  # x[0, 1, 2]

Reorders the first two axes while preserving the third axis, changing the shape from (2, 3, 4) to (3, 2, 4).

When to use it

Swap rows and columns of a 2-D array.

Matrix operations

Convert multidimensional data to the axis order required by an algorithm.

Tensor layout

Reverse or selectively reorder dimensions while preserving the array values.

Shape inspection

Interview questions

QWhat does np.transpose do by default?

np.transpose reorders an array's axes and returns the resulting array. Without axes, it uses the reversed axis order, so an array with shape (2, 3, 4) becomes (4, 3, 2).

Follow-up: What does an explicit permutation mean? For axes=(1, 0, 2), output axis 0 corresponds to input axis 1, output axis 1 to input axis 0, and output axis 2 to input axis 2.

QHow do you control the axis order?

For a two-dimensional array, it performs the standard matrix transpose. For an n-dimensional array, the supplied axes sequence determines the output-axis order.

Follow-up: How would you move the last axis to the front? For an array with ndim=N, use axes=(N-1, 0, 1, ..., N-2).

QWhat are the accepted input and output?

The function accepts array-like input and returns an ndarray with its axes permuted. Negative axis indices may also be used when specifying the permutation.

Follow-up: What constraint still applies with negative indices? After normalization, the values must still represent a complete permutation of the array's axes.

  • numpy.ndarray.transpose
  • numpy.ndarray.T
  • numpy.moveaxis
  • numpy.swapaxes

Source: numpy.transpose — NumPy v2.4 Manual. Checked 2026-10-15.