Python Program to find transpose of a matrix
Last Updated :
09 May, 2023
Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i].

For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension.
Python3
# Python3 Program to find
# transpose of a matrix
N = 4
# This function stores
# transpose of A[][] in B[][]
def transpose(A,B):
for i in range(N):
for j in range(N):
B[i][j] = A[j][i]
# driver code
A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
B = A[:][:] # To store result
transpose(A, B)
print("Result matrix is")
for i in range(N):
for j in range(N):
print(B[i][j], " ", end='')
print()
# This code is contributed by Anant Agarwal.
Output:Result matrix is
1 2 3 4
2 2 3 4
3 3 3 4
4 4 4 4
Time Complexity: O(n2)
Auxiliary Space: O(n2)
For Rectangular Matrix: The below program finds transpose of A[][] and stores the result in B[][].
Python3
# Python3 Program to find
# transpose of a matrix
M = 3
N = 4
# This function stores
# transpose of A[][] in B[][]
def transpose(A, B):
for i in range(N):
for j in range(M):
B[i][j] = A[j][i]
# driver code
A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]
# To store result
B = [[0 for x in range(M)] for y in range(N)]
transpose(A, B)
print("Result matrix is")
for i in range(N):
for j in range(M):
print(B[i][j], " ", end='')
print()
Output:Result matrix is
1 2 3
1 2 3
1 2 3
1 2 3
Time Complexity: O(n*m)
Auxiliary Space: O(n*m)
In-Place for Square Matrix:
Python3
# Python3 Program to find
# transpose of a matrix
N = 4
# Finds transpose of A[][] in-place
def transpose(A):
for i in range(N):
for j in range(i+1, N):
A[i][j], A[j][i] = A[j][i], A[i][j]
# driver code
A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
transpose(A)
print("Modified matrix is")
for i in range(N):
for j in range(N):
print(A[i][j], " ", end='')
print()
# This code is contributed by Anant Agarwal.
Output:Modified matrix is
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Time Complexity: O(n2)
Auxiliary Space: O(1)
Please refer complete article on Program to find transpose of a matrix for more details!
Approach#4: Using zip()
In this approach, zip(*matrix) is used to "unzip" the rows of the matrix and group the corresponding elements together. The * operator is used to pass the rows of the matrix as separate arguments to zip(). The resulting tuples are then converted back into lists using a list comprehension.
Algorithm
1. Define the matrix to be transposed.
2. Use the zip() function to group the corresponding elements of each row together and create tuples from them.
3. Convert each tuple back to a list using a list comprehension.
4. Store the resulting list of lists as the transposed matrix.
5. Print both the original and transposed matrices.
Python3
matrix = [[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
transpose = [list(row) for row in zip(*matrix)]
print("Original Matrix:")
for row in matrix:
print(row)
print("Transposed Matrix:")
for row in transpose:
print(row)
OutputOriginal Matrix:
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
[4, 4, 4, 4]
Transposed Matrix:
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]
Time Complexity:
The time complexity of this program is O(n^2) where n is the number of rows or columns in the matrix. This is because we need to access each element of the matrix exactly once to create the transposed matrix.
Space Complexity:
The space complexity of this program is also O(n^2). This is because we need to store the original matrix and the transposed matrix in memory, which both have n^2 elements. Additionally, we create temporary tuples during the transposition process, but these are discarded after they are converted back to lists.
METHOD 5:Using list comprehension
APPROACH:
This program demonstrates how to find the transpose of a given matrix using a list comprehension.
ALGORITHM:
1.Initialize a 2D list A with the given matrix values.
2.Create a new 2D list result using a nested list comprehension.
3.In the inner list comprehension, iterate through the rows of A and extract the ith element from each row.
4.Append the extracted elements as a row to the result list.
5.Print the result matrix by iterating through each row and joining the elements with a space.
Python3
A = [[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
result = [[row[i] for row in A] for i in range(len(A[0]))]
# Print the result
for row in result:
print(' '.join([str(elem) for elem in row]))
Output1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Time complexity: O(n^2) - as the program iterates through each element of the matrix A and creates a new matrix of the same size.
Auxiliary Space: O(n^2) - as the program creates a new matrix of the same size as A to store the transpose.
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