One should understand what a matrix is to know **how to transpose a matrix in Python. **A matrix is a two-dimensional data type. In mathematics, it is formed with rows and columns of numbers and variables. The transpose of a matrix can be formed when the rows and columns of the matrix are exchanged.

In Python, a matrix is created using nested lists. Along with the concept of lists, one will also need to be aware of how FOR loops are used in Python.

**Matrix Transpose using Nested Loop **

A nested list is a list inside a list. It is used to create a matrix such that each element of the nested list is an element of the row of the matrix.

Here is an example of a matrix.

X = [[1, 3], [4,5], [7, 9]].

It is a 3×2 matrix. That is, it has three rows and two columns. The indexing of the elements of the matrix, when done as per the indexing followed in Python will be such that the first element, that is, 1 will be indexed as X [0][0].

The transpose of this matrix will provide a matrix X’ that will be a 2×3 matrix. It will be formed by placing the element present at the ith row and jth column at the jth row and ith column in the X’.

The following code shows how to transpose a matrix in Python.

` ````
```X = [[1, 3], [4,5], [7, 9]]
result = [0, 0, 0], [0, 0, 0]]
for i in range (len (X)):
for j in range (len (X[0])):
result [j][i] = X [i][j]
for r in result:
print (r)

The program first considers that the transpose of the matrix has zero or some garbage value as each element. Then, using FOR loop, the elements of the X matrix are placed in the resulting transpose as required. Once all the elements are transposed, the resulting matrix is printed.

**Matrix Transpose using Nested List Comprehension **

Another method that requires lesser lines of code is nested list comprehension. The program to know how to transpose a matrix in Python is provided below.

` ````
```X = [[1, 3], [4,5], [7, 9]]
result = [[X[j][i] for j in range (len (X))] for i in range (len (X[0]))]
for r in result:
print (r)

The nested list comprehension is used in the code above to iterate through each element present in the matrix. Once all the elements are placed in the transpose, then the final transpose of the matrix is printed.

Thus, the basic concept to find the transpose of a matrix in Python is to place the X[i][j] element at the place of Y[j][i] where X is the original matrix and Y is the transpose of X.

## Frequently Asked Questions

**How do you transpose a matrix in Python?**

To transpose a matrix in Python, FOR loop is used. Each element of the matrix is iterated and placed at the respective place in the transpose. The placing is done such that the element at the ith row and jth column is placed at the jth row and ith column.

**How do you transpose in Python?**

To transpose in Python, one can either use the FOR loop with nested loops or use the nested list method. Both the methods provide the same result.

**How do you transpose a matrix?**

To transpose a matrix, the rows and columns of the matrix are interchanged with each other. Thus, a 1×3 matrix will form a transpose of 3×1.

**How do you transpose a matrix using NumPy in Python?**

NumPy in Python has a method pre-programmed in it to find the transpose of any matrix. The transpose () method is used in NumPy.