🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.swapaxes()

AI & DATA SCIENCE // np-swapaxes

np.swapaxes() swaps exactly two specified axes of an array, leaving every other axis in its original position.

Syntax

np.swapaxes(arr, axis1, axis2)

Deep Dive Course

Unlike np.transpose(), which reorders all axes at once, whether by full reversal or an explicit full permutation, swapaxes() targets exactly two axes by their integer position and exchanges them, leaving the rest of the array's structure untouched, which is often simpler to reason about when you only need to fix one specific pair of dimensions, rather than specifying a complete new ordering for every axis.

1Understanding np.swapaxes()

Unlike np.transpose(), which reorders all axes at once, whether by full reversal or an explicit full permutation, swapaxes() targets exactly two axes by their integer position and exchanges them, leaving the rest of the array's structure untouched, which is often simpler to reason about when you only need to fix one specific pair of dimensions, rather than specifying a complete new ordering for every axis.

💡

Use swapaxes() when you only need to exchange two specific axes and want to leave everything else alone — it avoids having to think through and write out a full axis-order tuple the way np.transpose() with explicit axes would require.

editor.html
import numpy as np

arr = np.arange(24).reshape(2, 3, 4)
swapped = np.swapaxes(arr, 0, 2)
print(swapped.shape)
localhost:3000

2Practical Example

Here is a real-world application of np.swapaxes() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.swapaxes(matrix, 0, 1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.swapaxes():

1. Use swapaxes() for a targeted two-axis swap instead of writing out a full axes tuple for np.transpose() when only two dimensions actually need to change

2. Verify the resulting .shape after any axis swap on higher-dimensional data, since axis positions are easy to miscount

3. Remember swapaxes() returns a view, not a copy, the same as transpose() and .T

⚠️

Tip: Use swapaxes() when you only need to exchange two specific axes and want to leave everything else alone — it avoids having to think through and write out a full axis-order tuple the way np.transpose() with explicit axes would require.

editor.html
import numpy as np

arr = np.arange(24).reshape(2, 3, 4)
swapped = np.swapaxes(arr, 0, 2)
print(swapped.shape)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.arange(24).reshape(2, 3, 4)
swapped = np.swapaxes(arr, 0, 2)
print(swapped.shape)
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.swapaxes(matrix, 0, 1))

Best Practices

  • Use swapaxes() for a targeted two-axis swap instead of writing out a full axes tuple for np.transpose() when only two dimensions actually need to change
  • Verify the resulting .shape after any axis swap on higher-dimensional data, since axis positions are easy to miscount
  • Remember swapaxes() returns a view, not a copy, the same as transpose() and .T

Interview Question

For a 2D array, how does np.swapaxes(arr, 0, 1) compare to arr.T?

Hint: Think about what each operation actually targets.

For exactly two dimensions, swapping axis 0 and axis 1 is precisely what a full transpose does, so np.swapaxes(arr, 0, 1) and arr.T produce identical results for a 2D array. The distinction only becomes meaningful for arrays with three or more dimensions, where swapaxes() lets you exchange just one specific pair of axes while leaving every other axis exactly where it was, whereas .T would reverse the order of all of them at once.

Exercises

MediumPractice using np.swapaxes() in a real scenario.
View Solution
import numpy as np

arr = np.arange(24).reshape(2, 3, 4)
swapped = np.swapaxes(arr, 0, 2)
print(swapped.shape)

Frequently Asked Questions

For a 2D array, how does np.swapaxes(arr, 0, 1) compare to arr.T?

For exactly two dimensions, swapping axis 0 and axis 1 is precisely what a full transpose does, so np.swapaxes(arr, 0, 1) and arr.T produce identical results for a 2D array. The distinction only becomes meaningful for arrays with three or more dimensions, where swapaxes() lets you exchange just one specific pair of axes while leaving every other axis exactly where it was, whereas .T would reverse the order of all of them at once.

Related Functions

np-transposendarray-tnp-reshape