Python Program to find transpose of a matrix
Last Updated :
25 Jun, 2025
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.

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)
Output1 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)
Output1 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)
Output1 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:
Similar Reads
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python Program to Convert Matrix to String Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string.Using List ComprehensionList comprehension provides a con
2 min read
Python Program for Rotate a Matrix by 180 degree Given a square matrix, the task is that turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input: 1 2 3 4 5 6 7 8 9 Output: 9 8 7 6 5 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output: 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1Python Program for Rotate a Matrix b
5 min read
Python Program to Multiply Two Matrices Given two matrices, we will have to create a program to multiply two matrices in Python. Example: Python Matrix Multiplication of Two-DimensionPythonmatrix_a = [[1, 2], [3, 4]] matrix_b = [[5, 6], [7, 8]] result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): result[i][j] = (matrix_a[i][0]
5 min read
Python Program to Reverse Every Kth row in a Matrix We are given a matrix (a list of lists) and an integer K. Our task is to reverse every Kth row in the matrix. For example:Input : a = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 4 Output : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [6, 3], [3, 7, 4], [2, 9]]Using reversed() and loopWe c
4 min read
Python Program to Construct n*m Matrix from List We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension
3 min read