Last Updated : 04 Jul, 2025
Transposing a matrix in Python means flipping it over its diagonal, turning all the rows into columns and all the columns into rows. For example, a matrix like [[1, 2], [3, 4], [5, 6]], which has 3 rows and 2 columns, becomes [[1, 3, 5], [2, 4, 6]], which has 2 rows and 3 columns after transposing. Let's understand different methods to do this efficiently.
Using List ComprehensionList comprehension is used to iterate through each element in the matrix. In the given example, we iterate through each element of matrix (m) in a column-major manner and assign the result to the rez matrix which is the transpose of m.
Python
m = [[1, 2], [3, 4], [5, 6]]
res = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
for row in res:
print(row)
[1, 3, 5] [2, 4, 6]
Explanation: This expression creates a new matrix by taking each column from the original as a row in the new one. It swaps rows with columns.
Using zipPython Zip returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. In this example we unzip our array using * and then zip it to get the transpose.
Python
m = [(1, 2, 3), (4, 5, 6),
(7, 8, 9), (10, 11, 12)]
t_m = zip(*m)
for row in t_m:
print(row)
(1, 4, 7, 10) (2, 5, 8, 11) (3, 6, 9, 12)
Explanation: This code transposes the matrix m using zip(*m). The * unpacks the rows and zip() groups elements column-wise. Each output tuple represents a column from the original matrix, effectively swapping rows and columns.
Using NumPyPython NumPy is a general-purpose array-processing package designed to efficiently manipulate large multi-dimensional arrays.
Example 1: The transpose method returns a transposed view of the passed multi-dimensional matrix.
Python
import numpy
m = [[1, 2, 3], [4, 5, 6]]
print(numpy.transpose(m))
[[1 4] [2 5] [3 6]]
Explanation: numpy.transpose() swap rows and columns of the matrix m. It converts the original matrix of 2 rows and 3 columns into one with 3 rows and 2 columns effectively transposing it.
Example 2: Using ".T" after the variable
Python
import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.T)
[[1 4] [2 5] [3 6]]
Explanation: This code uses NumPy to create a 2D array m, then prints its transpose using .T. The .T attribute swaps rows and columns, converting the original 2x3 matrix into a 3x2 transposed matrix.
Python itertools is a module that provides various functions that work on iterators to produce complex iterators. chain() is a function that takes a series of iterables and returns one iterable.
Python
from itertools import chain
import time
import numpy as np
def transpose2(M):
M = M.tolist()
n = len(M[0])
L = list(chain(*M))
return [L[i::n] for i in range(n)]
m = np.array([[1, 2, 3], [4, 5, 6]])
start = time.time_ns()
res = transpose2(m)
end = time.time_ns()
print(res)
print("Time taken", end - start, "ns")
[[1, 4], [2, 5], [3, 6]] Time taken 9813 ns
Explanation: It first converts the matrix to a list of lists, flattens it into a single list using chain(*M), then rebuilds the transposed matrix by slicing every n-th element.
Related articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4