Select Page

How to Find the Transpose of a Matrix in Python

by | Programming, Python, Tips

The transpose of a matrix is a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns.

We widely use the transpose of the matrix in linear algebra, physics, and computer science.

This tutorial will go through how to get the transpose of a matrix without NumPy and with NumPy, with the help of code examples.


What is the Transpose of a Matrix

We denote the transpose of a matrix A by A^{T}. For example, if:

3x2 Matrix
3×2 matrix

then the transpose of A is:

2x3 matrix
2×3 matrix

Transpose of a Matrix in Python Without NumPy

Creating a Matrix in Python Without NumPy

In Python, we can create a matrix as a nested list, which is a list within a list. Each element in a nested list is a row of the matrix, for example:

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

represents a 3×3 matrix. Putting each row on a separate line in your code will improve readability.

Creating a Matrix in Python With NumPy

We can create a multidimensional array using NumPy by passing a nested list to the array() method. We can treat this array as a matrix. Let’s look at an example:

import numpy as np

A = np.array([[10, 3, 5],
[7, 9, 2],
[11, 6, 9]], dtype=int)

print(A)
[[10  3  5]
 [ 7  9  2]
 [11  6  9]]

If you try to create a multidimensional numpy array where each row has a different length, Python will raise the error: ValueError: setting an array element with a sequence. Each row in a multidimensional numpy array must have the same length if the dtype is not object. To learn more about this ValueError, go to the article: How to Solve Python ValueError: setting an array element with a sequence.

Transpose of a Matrix in Python Using a Nested Loop

We can use a nested loop to iterate over the rows and columns of a nested list. To get the transpose of a matrix A, we place the element at the ith row and the jth column in the A at the jth row and the ith row in A_T. If A is a 2×3 matrix will be a 3×2 matrix.

A = [[1, 2],
[3, 4],
[5, 6]]

A_T = [[0, 0, 0],
       [0, 0, 0]]

# Iterate through rows
for i in range(len(A)):
    # Iterate through columns
    for j in range(len(A[0])):
        A_T[j][i] = A[i][j]

for i in A_T:
    print(i)

In the above program, we use nested loops to iterate through each row and column. At each iteration we place the element at A[i][j] into A_T[j][i].

[1, 3, 5]
[2, 4, 6]

Transpose of a Matrix in Python Using a Nested List Comprehension

Nested list comprehension performs a list comprehension within a list comprehension, resulting in a nested list. The syntax for nested list comprehension is:

new_list = [[expression for item in list] for item in list]

We can use nested list comprehension to get the transpose of a matrix, similar to the nested loop method. Let’s look at an example:

# Program to get the transpose of a matrix using nested list comprehension

# 2 x 3 matrix


A = [[1, 2],
[3, 4],
[5, 6]]

# A_T is a 3 x 2 matrix

A_T = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]

for i in A_T:
    print(i)

In the above code, the nested list comprehension iterates through the elements present in the matrix and places the elements of A[j][i] at the place A_T[i][j]. The first part of the nested list comprehension iterates over the columns in the matrix and the second part iterates over rows in the matrix. Let’s run the code to get the result:

[1, 3, 5]
[2, 4, 6]

Both nested loop approaches are computationally expensive and do not scale well to large matrices. For larger matrix operations, it is better to use numerical libraries like NumPy. The following example will show how to transpose a matrix using numpy.transpose().

Transpose of a Matrix in Python With NumPy

Transpose of a Matrix in Python Using a numpy.transpose()

We can use numpy to get the transpose of a matrix by converting a nested list to a numpy array and calling the transpose() method. Let’s look at an example below:

import numpy as np

A = np.array([[1, 2],
[3, 4],
[5, 6]])

A_T = A.transpose()

print(A_T)

Let’s run the code to get the result:

[[1 3 5]
 [2 4 6]]

We can also leave the matrix as a nested list and then pass it as a parameter to the numpy.transpose() method.

import numpy as np

A = [[1, 2],
[3, 4],
[5, 6]]

A_T = np.transpose(A)

print(A_T)

Let’s run the code to get the result:

[[1 3 5]
 [2 4 6]]

Summary

Congratulations on reading to the end of this tutorial! You have gone through how to get the transpose of a matrix with and without using NumPy.

For further reading on matrix manipulation in Python, go to the article: How to Multiply Two Matrices in Python.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!