Transpose: Rows Become Columns

Linear AlgebraEasy+10 XP+10

Transpose: Rows Become Columns

Transposing flips a matrix over its diagonal: row i becomes column i. An m × n matrix comes back as n × m.

Task: write transpose(matrix) that takes a list of rows and returns the transposed matrix, also as a list of rows.

  • An empty matrix returns an empty list.
  • Every row is guaranteed to be the same length.
  • Leave the numbers exactly as they are — no rounding, no type conversion.

This shows up constantly once you start writing gradients by hand. X @ W needs X.T to get the gradient with respect to W — the shapes only line up one way, and transpose is what lines them up.

The Transpose: Flip Rows and Columns

Drag a cell in M. Transposing doesn't just relabel — it can change how many vectors you have, and how many dimensions each one has.

M (2×3) — 3 vectors in a plane
2
3
1
1
2
3
-6.00-5.00-4.00-3.00-2.00-1.001.002.003.004.005.006.00-6.00-5.00-4.00-3.00-2.00-1.001.002.003.004.005.006.00
Mᵀ (3×2) — 2 vectors in space
2
1
3
2
1
3
-4.00-2.002.004.00-4.00-2.002.004.00-4.00-2.002.004.00xyz
Roll
Drag to spin · use the slider to roll

M's 3 columns were 2D vectors in a plane. Mᵀ's 2 columns (M's old rows) are 3D vectors in space — transposing changed both the count and the dimension.

Examples

matrix = [[1, 2, 3], [4, 5, 6]]
returns [[1, 4], [2, 5], [3, 6]]
matrix = [[1], [2], [3]]
returns [[1, 2, 3]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run