Transposing & Swapping Axes
Transposing an array reorders its dimensions — for a 2D array it flips rows and columns — and NumPy does it as a cheap view that shares the original data instead of copying it.
Learn Transposing & Swapping Axes in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll transpose matrices with .T and np.transpose, reorder the axes of 3D arrays, swap a pair of axes with swapaxes, relocate an axis with moveaxis, and learn that all of these return views.
For a 2D array, the .T attribute swaps the two axes: element (i, j) becomes (j, i) , so a shape (2, 3) array becomes (3, 2) . It is the classic matrix transpose. Because it is a view, it costs almost nothing even on large arrays.
For arrays with more than two dimensions, .T simply reverses every axis, which is often not what you want. np.transpose(arr, axes) lets you specify the exact new order as a permutation of axis numbers. For a 3D array of shape (2, 3, 4) , passing (1, 0, 2) gives shape (3, 2, 4) .
When you only want to exchange two axes, np.swapaxes(arr, a, b) is the clearest tool. When you want to relocate an axis to a new position while the others shift to fill the gap, use np.moveaxis(arr, source, destination) . All of these — like .T and np.transpose — return a view , so edits flow back to the original.
Transpose the matrix so its shape becomes (3, 2) by filling in the attribute.
Answer: T (so m.T ). Equivalent calls are np.transpose(m) and np.swapaxes(m, 0, 1) .
❌ Editing a transpose and surprising the original
Transpose returns a view, so writes propagate back to the source array.
✅ Fix: use arr.T.copy() when you need an independent array to modify.
❌ Using .T on a 3D array and getting the wrong order
On 3D+ arrays .T reverses every axis, which is rarely the intended reorder.
✅ Fix: pass an explicit order to np.transpose(arr, (...)) or use np.swapaxes / np.moveaxis .
Image data often comes as (height, width, channels) but some libraries want (channels, height, width). Convert between the two with moveaxis.
Lesson complete — you can reorder any axis!
You can transpose with .T , reorder axes precisely with np.transpose , swap a pair with np.swapaxes , relocate one with np.moveaxis , and you know all of these return views.
🚀 Up next: Dates & Times with datetime64 — store and compute with calendar dates inside NumPy arrays.
Practice quiz
For a 2D array of shape (2, 3), what shape does .T give?
- (2, 3)
- (6,)
- (3, 2)
- (2, 2)
Answer: (3, 2). .T swaps the two axes, turning (2, 3) into (3, 2).
Does transposing an array copy the data?
- No, it returns a view sharing the same data
- Yes, always a full copy
- Only for 1D arrays
- Only when the array is large
Answer: No, it returns a view sharing the same data. .T and np.transpose return a view; only shape and strides change.
What does .T do to a 1D array like [1, 2, 3]?
- Raises an error
- Leaves it unchanged (still 1D)
- Makes it a column
- Reverses the values
Answer: Leaves it unchanged (still 1D). A 1D array has one axis, so .T is a no-op; shape stays (3,).
For a (2, 3, 4) array, what shape does np.transpose(a, (1, 0, 2)) give?
- (2, 3, 4)
- (4, 3, 2)
- (2, 4, 3)
- (3, 2, 4)
Answer: (3, 2, 4). The permutation puts old axis 1 first, old axis 0 second, axis 2 last.
What does np.transpose(a) with no axes argument do?
- Returns a copy
- Keeps the shape
- Reverses the order of all axes
- Swaps only the first two axes
Answer: Reverses the order of all axes. With no axes it reverses every dimension, the same as a.T.
Which function exchanges exactly two axes?
- np.moveaxis
- np.swapaxes
- np.reshape
- np.ravel
Answer: np.swapaxes. np.swapaxes(arr, a, b) exchanges exactly two axes.
Which function relocates an axis to a new position?
- np.moveaxis
- np.swapaxes
- np.flatten
- np.tile
Answer: np.moveaxis. np.moveaxis slides axes from source to destination positions.
After t = m.T, what happens if you write t[0, 1] = 99?
- Nothing changes
- It raises an error
- Only t changes
- m changes too, because t is a view
Answer: m changes too, because t is a view. Transpose returns a view, so writes flow back to the original m.
For a (2, 3, 4) array, what shape does np.swapaxes(a, 0, 2) give?
- (3, 2, 4)
- (4, 3, 2)
- (2, 4, 3)
- (2, 3, 4)
Answer: (4, 3, 2). Swapping axes 0 and 2 turns (2, 3, 4) into (4, 3, 2).
To convert an image from (H, W, C) to (C, H, W), which is cleanest?
- a.T
- np.reshape(a, (3, 4, 5))
- np.moveaxis(img, -1, 0)
- np.swapaxes(img, 0, 1)
Answer: np.moveaxis(img, -1, 0). moveaxis(img, -1, 0) moves the channel axis to the front.