🚀 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...

ndarray.T

AI & DATA SCIENCE // ndarray-t

ndarray.T returns the array's transpose — its axes reversed, so for a 2D array, rows become columns and columns become rows.

Syntax

arr.T

Deep Dive Course

For a 2D array, .T swaps rows and columns, turning a shape of (m, n) into (n, m) — element (i, j) in the original array becomes element (j, i) in the transposed one. For a 1D array, .T has no effect, since there's only one axis to reverse. Like most NumPy attribute-based operations, .T returns a view of the original data rather than a copy whenever possible, meaning modifying the transposed array's elements also modifies the original array's underlying data.

1Understanding ndarray.T

For a 2D array, .T swaps rows and columns, turning a shape of (m, n) into (n, m) — element (i, j) in the original array becomes element (j, i) in the transposed one. For a 1D array, .T has no effect, since there's only one axis to reverse. Like most NumPy attribute-based operations, .T returns a view of the original data rather than a copy whenever possible, meaning modifying the transposed array's elements also modifies the original array's underlying data.

💡

arr.T is a view, not a copy — modifying elements through the transposed array changes the original array's data too, which is efficient but can be a surprising source of bugs if you expected an independent copy.

editor.html
import numpy as np

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

2Practical Example

Here is a real-world application of ndarray.T showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
transposed = matrix.T
transposed[0, 0] = 99
print(matrix)
localhost:3000

3Best Practices

Follow these guidelines when working with ndarray.T:

1. Use .T for a quick transpose of a 2D array instead of manually swapping axes with np.transpose() when you don't need to specify a custom axis order

2. Call .copy() on the result of .T explicitly when you need an independent transposed array that won't affect the original if modified

3. Use np.transpose() instead of .T for arrays with more than 2 dimensions, where 'transpose' needs an explicit axis order rather than a simple reversal

⚠️

Tip: arr.T is a view, not a copy — modifying elements through the transposed array changes the original array's data too, which is efficient but can be a surprising source of bugs if you expected an independent copy.

editor.html
import numpy as np

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

Examples

Example 01Basic Usage
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
transposed = matrix.T
transposed[0, 0] = 99
print(matrix)

Best Practices

  • Use .T for a quick transpose of a 2D array instead of manually swapping axes with np.transpose() when you don't need to specify a custom axis order
  • Call .copy() on the result of .T explicitly when you need an independent transposed array that won't affect the original if modified
  • Use np.transpose() instead of .T for arrays with more than 2 dimensions, where 'transpose' needs an explicit axis order rather than a simple reversal

Interview Question

Why does modifying an element in arr.T also change the corresponding element in the original array arr?

Hint: Think about whether .T copies the underlying data or just changes how it's viewed.

arr.T doesn't copy any data — it returns a new array object that shares the exact same underlying memory buffer as arr, just with the shape and strides, the step sizes used to navigate memory, adjusted to read that same data in transposed order. Since both arrays point at the same underlying bytes, writing to an element through either one changes the value that the other one sees too. To get an independent transposed array, you need to explicitly call .copy() on the result.

Exercises

MediumPractice using ndarray.T in a real scenario.
View Solution
import numpy as np

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

Frequently Asked Questions

Why does modifying an element in arr.T also change the corresponding element in the original array arr?

arr.T doesn't copy any data — it returns a new array object that shares the exact same underlying memory buffer as arr, just with the shape and strides, the step sizes used to navigate memory, adjusted to read that same data in transposed order. Since both arrays point at the same underlying bytes, writing to an element through either one changes the value that the other one sees too. To get an independent transposed array, you need to explicitly call .copy() on the result.

Related Functions

np-transposenp-swapaxesndarray-shape