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

NumPy · Function

np.moveaxis

Moves selected axes of an array to new positions while preserving the relative order of axes that are not moved. The result is a view of the input array.

Since
1.11.0

How it works

np.moveaxis returns an array whose selected axes are relocated to specified destination positions. The axes not selected retain their relative order, and the result is a view of the input rather than an independently copied array.

Syntax

numpy.moveaxis(a, source, destination)

Parameters

NameTypeDescription
anp.ndarrayThe array whose axes should be reordered.
sourceint or sequence of intOriginal positions of the axes to move. Entries must be unique.
destinationint or sequence of intDestination positions for each original axis. Entries must be unique.

Returns

result — np.ndarray: An array with moved axes; this is a view of the input array.

Key points

  • Relocates one or more dimensions to explicitly chosen positions.
  • Dimensions that are not moved keep their relative ordering.
  • The source argument can identify one axis or a sequence of axes.
  • The destination argument can specify one target or multiple targets.
  • The function returns an array with the requested axis arrangement.
  • The result shares the input's underlying data rather than requiring a copy.

Common mistakes

  • Each source axis must appear only once; duplicate source entries are invalid.
  • Destination positions must not repeat, so each moved axis needs its own target position.

Example: Move an axis

import numpy as np

x = np.zeros((3, 4, 5))
y = np.moveaxis(x, 0, -1)
print(y.shape)  # (4, 5, 3)

Moves the first axis to the end while retaining the relative order of the other axes.

When to use it

Reposition a feature axis

A tensor stores channels or features in an inconvenient dimension and downstream code expects them elsewhere.

Rearrange multiple axes

Several dimensions need coordinated placement while untouched dimensions should keep their ordering.

Create a reordered view

An array must be reordered without deliberately allocating a separate data copy.

Interview questions

QWhat does np.moveaxis do?

It reorders selected axes to requested positions while preserving the order of all other axes. The returned array is a view of the input.

Follow-up: How can several axes be moved together? Pass sequences for source and destination, pairing each source axis with its corresponding destination.

QWhat shape results from np.moveaxis(x, 0, -1) when x has shape (3, 4, 5)?

For an array with shape (3, 4, 5), moving axis 0 to -1 produces shape (4, 5, 3). Negative positions are accepted as demonstrated by the documented examples.

Follow-up: What must be checked when using multiple axes? Source and destination entries must each be unique.

QHow does np.moveaxis relate to transpose and swapaxes?

Use np.moveaxis when specific axes should be relocated and the remaining axes should retain their order. np.swapaxes is the related operation for interchanging two axes, while np.transpose provides broader axis permutation.

Follow-up: When is swapaxes more direct? When exactly two axes need to be interchanged rather than positioned independently.

  • swapaxes
  • transpose
  • rollaxis

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