Learn Programming and technology with ease @ developerpublish.com

HomePythonPython Program to Find Transpose of a Matrix

# Python Program to Find Transpose of a Matrix

In this python tutorial, you will learn how to Find Transpose of a Matrix with for loop of the python programming language.

## How to Find Transpose of a Matrix?

Letâ€™s take a look at the source code,Â  here the values are assigned in the code, the for loop carry out the function.

```# Python Program to Find Transpose of a Matrix

X = [[12,7],
[4 ,5],
[3 ,8]]

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)
```

OUTPUT:

```[12, 4, 3]
[7, 5, 8]```
1. At first, we enter the values of two matrices. The first one with 3 rows and 2 columns and assign that matrix to the variable `X`. The second one with 2 rows and 3 columns and assign that matrix to the variable Â `result`.
2. We declare a `for` loop where the `range` for the variable `i` is `(len(X))`
3. We declare a `for` loop where the `range` for the variable `j` is `(len(X[0]))` for which we give the condition `result[j][i] = X[i][j]`
4. We declare the finalÂ `for` loop where the `result` is the variable `r` after which we give a `print` function which will display the values stored in the variable `r`
5. In this program, we have used nested `for` loop to iterate through each row and each column. At each point we place the `X[i][j]` element into `result[j][i]`

NOTE:

• TheÂ printÂ function is used to display the value, it prints the specified message to the screen
• Â TheÂ For loopsÂ are used to loop through an iterable object and perform the same action for each entry.
• TheÂ len()Â is a built function Â used to calculate the length of any iterable object

### You May Also Like

#### Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...