Signup/Sign In

Python Programs to Transpose a matrix in Single line

In this tutorial, we will learn to transpose a matrix in a single line in Python. Python does not have a built-in type for matrices but we can treat a list of a list as a matrix. The List is an ordered set of values enclosed in square brackets [ ].

Let A= [ [1, 2, 3] [4, 5, 6] [7, 8, 9] ] be a list of a list, then it can be treated as a 3x3 matrix with 3 rows and 3 columns.

[ [1, 2, 3]
  [4, 5, 6]
  [7, 8, 9] ] 

The transpose of this matrix will be a switch between the row and column indices of the matrix.

[ [1, 4, 7]
  [2, 5, 8]
  [3, 6, 9] ] 

In Python, we can use nested loops to find the transpose of a matrix but to perform this task in a single line there are some other interesting approaches that will be discussed below. For example,

Input: [ [1,3,4], [2,5,6], [3,7,8]]

Output: [ [1,2,3], [3,5,7], [4,6,8] ]

To implement transpose of a matrix in a single line we can follow these approaches:

  1. By using nested list comprehension
  2. By using NumPy module
  3. By using zip() function

Approach 1: Using nested list comprehension

List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We will use this to iterate through the rows and columns to switch them and store them in a new list. List comprehension allows us to write concise codes in Python.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Declare nested list for a matrix

Step 2- Print the original matrix

Step 3- To find transpose, use list comprehension

Step 4- Switch the rows with columns and store them in a new list

Step 5- Print the new list (transpose list)

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. Use a list comprehension to switch the rows with the columns.

matrix= [ [2,3],[4,5],[7,8]]

for m in matrix:
    print(m)

transpose= [ [matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print("Transpose: ")

for t in transpose:
    print(t)


[2, 3]
[4, 5]
[7, 8]
Transpose:
[2, 4, 7]
[3, 5, 8]

Approach 2: Using NumPy module

NumPy module is a python package for the computation and processing of the multidimensional and single-dimensional list elements. We will use the transpose() function of the NumPy module. It returns the transpose of the list.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Import NumPy module in the program

Step 2- Declare nested list for a matrix

Step 3- Print the original matrix

Step 4- To find transpose, use transpose()

Step 5- Print the new list this will be the transpose

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach.

import numpy

matrix= [ [5,2,3],[6,7,8],[3,4,9]]
for m in matrix:
    print(m)

# print transpose
print("Transpose: ")
print(numpy.transpose(matrix))

In place of transpose(), we can also use T attribute. The T attribute is used to get the transpose of the array. But this only works for NumPy arrays.

import numpy

matrix = numpy.array([[1,2,3],[4,5,6]])
#print original matrix
for m in matrix:
    print(m)
print("transpose")
print(matrix.T)


[5, 2, 3]
[6, 7, 8]
[3, 4, 9]
Transpose:
[[5 6 3]
[2 7 4]
[3 8 9]]

Approach 3: zip()

The zip() function returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences. In this example, we unzip our array using * and then zip it to get the transpose.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Declare nested list for a matrix

Step 2- Print the original matrix

Step 3- To find transpose, first unzip using *

Step 4- zip the array using zip() function

Step 5- Store the value returned by zip() in another list

Step 6- Print the new list this will be the transpose

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach.

matrix=[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]

for m in matrix:
    print(m)

#transpose
print("Transpose:")
transpose = zip(*matrix)

#print transpose matrix
for t in transpose:
    print(t)


(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(10, 11, 12)
Transpose:
(1, 4, 7, 10)
(2, 5, 8, 11)
(3, 6, 9, 12)

Conclusion

In this tutorial, we have seen how to find transpose in a single line in Python. We have discussed three approaches for performing this task. First, we have used nested list comprehension, the second was by using the Numpy module and at last, we have seen the zip() function.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.