Open In App

Python Program to find transpose of a matrix

Last Updated : 25 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Transpose of a matrix involves converting its rows into columns and columns into rows. For example, if we have a matrix with values [[1, 2, 3], [4, 5, 6], [7, 8, 9]], its transpose would be [[1, 4, 7], [2, 5, 8], [3, 6, 9]]. Let's explore different methods to perform this efficiently.

matrix-transpose

Using zip()

This approach works by unpacking each row and grouping elements at the same index across all rows. It creates a new transposed matrix and works perfectly for both square and rectangular matrices. It’s a great choice for quick and easy data transformations.

Python
m = [[1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]

res = [list(row) for row in zip(*m)]
for row in res:
    print(*row)

Output
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Explanation: zip() with *m unpacks matrix rows to group columns, transposing the matrix. list(row) converts tuples to lists and a loop prints each row with space-separated values.

Time Complexity: O(n * m)
Auxiliary Space: O(n * m)

Using in-place transpose

This method transposes a square matrix in-place by swapping elements across the diagonal. It's fast and memory-efficient since it doesn't create a new matrix, but it only works for square matrices, not rectangular ones.

Python
def transpose(m):
    n = len(m)
    for i in range(n):
        for j in range(i + 1, n):
            m[i][j], m[j][i] = m[j][i], m[i][j]

m = [[1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]

transpose(m)
for row in m:
    print(*row)

Output
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Explanation: fun(m) function uses nested loops to swap elements above the diagonal with those below (m[i][j] ↔ m[j][i]), transposing the matrix in-place. Starting j from i + 1 avoids redundant swaps. A final loop prints the transposed matrix row by row.

Time Complexity: O(n^2)
Auxiliary Space: O(1)

Using nested loops

This approach uses nested loops to build a new transposed matrix manually. It's great for learning and works for both square and rectangular matrices. Though less concise than zip(), it offers more control.


Python
def fun(m):
    r, c = len(m), len(m[0])
    t = [[0] * r for _ in range(c)]
    for i in range(r):
        for j in range(c):
            t[j][i] = m[i][j]
    return t

m = [[1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]]

res = fun(m)
for row in res:
    print(*row)

Output
1 2 3
1 2 3
1 2 3
1 2 3

Explanation: fun(m) gets the original matrix's dimensions, then creates a new matrix t of size c × r. Nested loops assign t[j][i] = m[i][j], swapping rows and columns to produce the transposed matrix.

Time Complexity: O(r * c)
Auxiliary Space: O(r * c)

Using numpy

NumPy offers an efficient way to transpose a matrix using the .T attribute, allowing instant and high-performance transposition. While it requires the NumPy package, it's ideal for handling large datasets.

Python
import numpy as np
a = np.array([
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]])

res = a.T 
print(res)

Output
[[1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]]

Explanation: a is converted to a NumPy array and a.T transposes it by swapping rows and columns instantly.

Time Complexity: O(r * c)
Auxiliary Space: O(r * c)

Related Articles:


    Practice Tags :

    Similar Reads