Tutorial

NumPy Matrix transpose() - Transpose of an Array in Python

Comprehensive guide to numpy transpose: how to transpose arrays, list-of-lists, 1D vs 2D behavior, 3D axis order, view vs copy, and differences from reshape and swapaxes.

Drake Nguyen

Founder · System Architect

3 min read
NumPy Matrix transpose() - Transpose of an Array in Python
NumPy Matrix transpose() - Transpose of an Array in Python

What is a transpose and why use it?

The transpose of a matrix or array switches rows with columns: an array with shape (m, n) becomes (n, m). In NumPy this operation is commonly called numpy transpose and is useful when you need to change rows to columns, adapt data for linear algebra routines, or reorder axes for broadcasting.

Basic numpy transpose for 2D arrays

To transpose a NumPy 2D array you can use np.transpose() or the .T attribute. The following numpy transpose 2d array example shows both approaches and the expected result.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print('Original shape:', arr.shape)
print(arr)

# using function
arr_t = np.transpose(arr)
# using attribute
arr_T = arr.T

print('\nTransposed shape:', arr_t.shape)
print(arr_t)

Output:

Original shape: (2, 3)
[[1 2 3]
 [4 5 6]]

Transposed shape: (3, 2)
[[1 4]
 [2 5]
 [3 6]]

Transpose an array-like object (list of lists)

numpy.transpose works on array-like objects as well, so you can pass a nested list directly. This makes it easy to transpose a list-of-lists without converting manually to an ndarray first.

lst = [[1, 2, 3], [4, 5, 6]]
transposed = np.transpose(lst)
print(transposed)

How transpose works for 1D arrays

1D arrays in NumPy have shape (n,) and are treated as neither row nor column vectors. Applying numpy transpose to a 1d array returns the same object because there are no two axes to swap. If you need a column vector, convert to 2D first:

a = np.array([1, 2, 3])
print(a.shape)        # (3,)
print(np.transpose(a).shape)  # still (3,)

# make it 2D then transpose
col = a[:, np.newaxis]   # shape (3,1)
print(col.T.shape)       # shape (1,3)

Transposing 3D arrays and axis order (0,1,2)

For arrays with more than two dimensions you can specify the axis permutation. The axis order (0, 1, 2) is the identity, but swapping axes reorders dimensions. Example for a 3D array:

arr3d = np.arange(24).reshape(2, 3, 4)  # shape (2,3,4)
# default transpose reverses axes: (4,3,2)
default_t = np.transpose(arr3d)
# custom axis order - move axis 0 to position 2
custom = np.transpose(arr3d, axes=(1, 2, 0))
print('default:', default_t.shape)
print('custom:', custom.shape)

Use this to control the mapping of indices for multidimensional data. Example long-tail queries: transpose of 3d numpy array, numpy transpose axis order (0,1,2), or numpy transpose multiple axes example.

Does numpy transpose return a view or copy?

Often numpy transpose returns a view: it adjusts the strides and shape without copying data. You can test this by inspecting the base attribute or using np.may_share_memory. If the layout requires a different memory order, NumPy will make a copy.

a = np.arange(6).reshape(2, 3)
b = a.T
print(b.base is a)                       # True for a view
print(np.may_share_memory(a, b))         # usually True

# operations that force a copy
c = np.transpose(a, axes=(1, 0)).copy()
print(c.base is None)                    # True if it's an independent array

numpy.transpose vs .T vs swapaxes vs reshape

  • np.transpose — flexible function that accepts an axes tuple for multidimensional arrays.
  • .T — short attribute for reversing axes; convenient for 2D arrays (equivalent to arr.transpose() for many cases).
  • swapaxes — swap two specific axes: useful when you need to exchange axis i and j only.
  • reshape — changes the shape without permuting elements; it is not a general substitute for transpose.

Practical tips and common questions

  • Use numpy transpose array operations when you need to change rows to columns or perform matrix transpose in python.
  • For large arrays prefer views (.T or np.transpose that returns a view) to avoid copying memory.
  • To ensure a copy, call .copy() after transpose.
  • Remember: transpose numpy array vs reshape — reshape changes shape but keeps element order; transpose permutes axes.

Summary

numpy transpose, np.transpose, and the .T attribute provide simple, efficient ways to flip axes and change shape from (m, n) to (n, m). Whether working with a numpy transpose matrix, a list of lists, or higher-dimensional arrays, understanding axis order and view vs copy behavior will help you write correct and performant code.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.