Transpose of a matrix is a task we all can perform very easily in Python (Using a nested loop). But there are some interesting ways to do the same in a single line. In Python, we can implement a matrix as a nested list (a list inside a list). Each element is treated as a row of the matrix. For example m = [[1, 2], [4, 5], [3, 6]] represents a matrix of 3 rows and 2 columns. The first element of the list – m[0] and the element in the first row, first column – m[0][0].
Example
Input: [[1,2],[3,4],[5,6]]
Output: [[1,3,5],[2,4,6]]
Explanation: Suppose we are given a matrix
[[1, 2],
[3, 4],
[5, 6]]
Then the transpose of the given matrix will be,
[[1, 3, 5],
[2, 4, 6]]
Python Programs to Transpose a Matrix in Single Line
There are various approaches to find the Transpose of a matrix in a single line in Python. In this article, we will discuss all the approaches which are specific to transposing a matrix in a single line in Python.
- Using List Comprehension
- Using zip
- Using NumPy
- Using itertools
Transpose Matrix In Single Line using List Comprehension
List 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.
Python3
m = [[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]] for row in m: print (row) rez = [[m[j][i] for j in range ( len (m))] for i in range ( len (m[ 0 ]))] print ( "\n" ) for row in rez: print (row) |
Output:
[1, 2]
[3, 4]
[5, 6]
[1, 3, 5]
[2, 4, 6]
Transpose a matrix in Single line in Python using zip
Python 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.
Python3
matrix = [( 1 , 2 , 3 ), ( 4 , 5 , 6 ), ( 7 , 8 , 9 ), ( 10 , 11 , 12 )] for row in matrix: print (row) print ( "\n" ) t_matrix = zip ( * matrix) for row in t_matrix: print (row) |
Output:
Note :- If you want your result in the form [[1,4,7,10][2,5,8,11][3,6,9,12]] , you can use t_matrix=map(list, zip(*matrix)).
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(10, 11, 12)
(1, 4, 7, 10)
(2, 5, 8, 11)
(3, 6, 9, 12)
Python Programs to Transpose a matrix using NumPy
Python 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.
Python3
import numpy matrix = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]] print (matrix) print ( "\n" ) print (numpy.transpose(matrix)) |
Output:
[[1, 2, 3], [4, 5, 6]]
[[1 4]
[2 5]
[3 6]]
Example 2: Using “.T” after the variable
Python3
import numpy as np matrix = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) print (matrix) print ( "\n" ) print (matrix.T) |
Output:
As you can see that both the output are same.
[[1 2 3] [4 5 6]]
[[1 4]
[2 5]
[3 6]]
Fastest way to Transpose a Matrix using Itertools
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.
In this example, we are using chain() function to convert all the values inside the matrix into a single list and then transpose the matrix. This method is way faster then other methods.
Python3
from itertools import chain import time import numpy as np def transpose2(M): n = len (M[ 0 ]) L = list (chain( * M)) return [L[i::n] for i in range (n)] start = time.time_ns() matrix = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) end = time.time_ns() print (transpose2(matrix)) print ( "Time taken" , end - start, "ns" ) |
Output
[[1, 4], [2, 5], [3, 6]]
Time taken 108570 ns