How to Transpose a matrix in Single line in Python?


In this article, we will show you how to transpose a matrix in a single line in python. Below are the various ways to accomplish this task −

  • Using Nested List Comprehension
  • Using NumPy Module
  • Using zip() Function

What is the transpose of the matrix?

A matrix transpose is the interchanging of rows and columns. It is abbreviated to A'. The element in the ith row and jth column of A' will be moved to the jth row and ith column of A'


Using Nested List Comprehension

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input matrix.

  • Print the given input matrix by traversing through each element in the input matrix using the for loop and printing the corresponding element.

  • Traverse until the number of rows is reached using a for loop and the range() function(returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number), then inside the for loop, traverse until the number of columns is reached using another for loop and len() function(The number of items in an object is returned by the len() method), and store the element in the list using Nested List Comprehension.

  • Print the transpose of an input matrix by traversing through each element in the above-got transpose matrix using the for loop and printing the corresponding element.

Example

The following program returns the transpose of an input matrix using nested List comprehension −

# input matrix inputMatrix= [[1, 4, 2],[2, 0, 3]] # printing input matrix print("Input matrix:") # traversing through each element in the input matrix for i in inputMatrix: # printing element print(i) # Transposing the matrix # Iterating till number of rows using for loop(n iterator) and # till number of columns using another for loop(m iterator) and storing the element(inputMatrix[n][m]) transposeMatrix= [ [inputMatrix[n][m] for n in range(len(inputMatrix))] for m in range(len(inputMatrix[0]))] # printing transpose of a matrix print("Transpose of an input matrix:") # traversing through each element in the above transpose matrix for i in transposeMatrix: # printing corresponding element print(i)

Output

On executing, the above program will generate the following output −

Input matrix:
[1, 4, 2]
[2, 0, 3]
Transpose of an input matrix:
[1, 2]
[4, 0]
[2, 3]

Using NumPy Module

NumPy is a Python library that computes and processes multidimensional and single-dimensional list elements. The NumPy module's transpose() function will be used here. It returns the transpose of a list.

NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage. It also improves the way data is handled for the process. In NumPy, we may generate an n-dimensional array. To use NumPy, we simply import it into our program, and then we can easily use NumPy's functionality in our code.

NumPy is a popular Python package for scientific and statistical analysis. NumPy arrays are grids of values from the same data type.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the NumPy module.

  • Print the given input matrix by traversing through each element in the input matrix using the for loop and printing the corresponding element.

  • Use the transpose() function(returns the transpose of a list) of a NumPy module to get the transpose input matrix by passing the input matrix as an argument to it and create a variable to store it.

  • Print the resultant transpose of an input matrix.

Example

The following program returns the transpose of an input matrix using NumPy.transpose() function −

# importing numpy module import numpy # input matrix inputMatrix= [[1, 4, 2],[2, 0, 3]] # printing input matrix print("Input matrix:") # traversing through each element in the input matrix for i in inputMatrix: # printing element print(i) # getting the transpose an input matrix transposeMatrix = numpy.transpose(inputMatrix) # printing transpose of a matrix print("Transpose of an input matrix:\n", transposeMatrix)

Output

On executing, the above program will generate the following output −

Input matrix:
[1, 4, 2]
[2, 0, 3]
Transpose of an input matrix:
[[1 2]
 [4 0]
 [2 3]]

Using zip() Function

The Python zip() function returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences.

Here we use * to unzip our array and then zip it to get the transpose.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input matrix.

  • Print the given input matrix by traversing through each element in the input matrix using the for loop and printing the corresponding element.

  • Use the zip() function to zip the input array(Here it transposes the matrix).

  • Print the transpose of an input matrix.

Example

The following program returns the transpose of an input matrix using the zip() function −

# input matrix inputMatrix= [(1, 4, 2),(2, 0, 3), (4, 2, 5)] # printing input matrix print("Input matrix:") # traversing through each element in the input matrix for i in inputMatrix: # printing element print(i) # getting the transpose an input matrix transposeMatrix = zip(*inputMatrix) # printing transpose of a matrix print("Transpose of an input matrix:") # traversing through each element in the above transpose matrix for i in transposeMatrix: # printing corresponding element at the iterator index print(i)

Output

On executing, the above program will generate the following output −

Input matrix:
(1, 4, 2)
(2, 0, 3)
(4, 2, 5)
Transpose of an input matrix:
(1, 2, 4)
(4, 0, 2)
(2, 3, 5)

Conclusion

In this article, we learned how to find transpose in Python in a single line. We used three approaches to doing this work. We began with nested list comprehension, then moved on to the Numpy module, and finally to the zip() method.


Advertisements