# Python Program to find transpose of a matrix

Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i].

**For Square Matrix :**

The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension.

`# Python3 Program to find ` `# transpose of a matrix ` ` ` `N ` `=` `4` ` ` `# This function stores ` `# transpose of A[][] in B[][] ` ` ` `def` `transpose(A,B): ` ` ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(N): ` ` ` `B[i][j] ` `=` `A[j][i] ` ` ` `# driver code ` `A ` `=` `[ [` `1` `, ` `1` `, ` `1` `, ` `1` `], ` ` ` `[` `2` `, ` `2` `, ` `2` `, ` `2` `], ` ` ` `[` `3` `, ` `3` `, ` `3` `, ` `3` `], ` ` ` `[` `4` `, ` `4` `, ` `4` `, ` `4` `]] ` ` ` ` ` `B ` `=` `A[:][:] ` `# To store result ` ` ` `transpose(A, B) ` ` ` `print` `(` `"Result matrix is"` `) ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(N): ` ` ` `print` `(B[i][j], ` `" "` `, end` `=` `'') ` ` ` `print` `() ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

**Output:**

Result matrix is 1 2 3 4 2 2 3 4 3 3 3 4 4 4 4 4

**For Rectangular Matrix :**

The below program finds transpose of A[][] and stores the result in B[][].

`# Python3 Program to find ` `# transpose of a matrix ` ` ` `M ` `=` `3` `N ` `=` `4` ` ` `# This function stores ` `# transpose of A[][] in B[][] ` ` ` `def` `transpose(A, B): ` ` ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(M): ` ` ` `B[i][j] ` `=` `A[j][i] ` ` ` `# driver code ` `A ` `=` `[ [` `1` `, ` `1` `, ` `1` `, ` `1` `], ` ` ` `[` `2` `, ` `2` `, ` `2` `, ` `2` `], ` ` ` `[` `3` `, ` `3` `, ` `3` `, ` `3` `]] ` ` ` ` ` `# To store result ` `B ` `=` `[[` `0` `for` `x ` `in` `range` `(M)] ` `for` `y ` `in` `range` `(N)] ` ` ` `transpose(A, B) ` ` ` `print` `(` `"Result matrix is"` `) ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(M): ` ` ` `print` `(B[i][j], ` `" "` `, end` `=` `'') ` ` ` `print` `() ` |

*chevron_right*

*filter_none*

**Output:**

Result matrix is 1 2 3 1 2 3 1 2 3 1 2 3

**In-Place for Square Matrix:**

`# Python3 Program to find ` `# transpose of a matrix ` ` ` `N ` `=` `4` ` ` `# Finds transpose of A[][] in-place ` `def` `transpose(A): ` ` ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(i` `+` `1` `, N): ` ` ` `A[i][j], A[j][i] ` `=` `A[j][i], A[i][j] ` ` ` `# driver code ` `A ` `=` `[ [` `1` `, ` `1` `, ` `1` `, ` `1` `], ` ` ` `[` `2` `, ` `2` `, ` `2` `, ` `2` `], ` ` ` `[` `3` `, ` `3` `, ` `3` `, ` `3` `], ` ` ` `[` `4` `, ` `4` `, ` `4` `, ` `4` `]] ` ` ` `transpose(A) ` ` ` `print` `(` `"Modified matrix is"` `) ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(N): ` ` ` `print` `(A[i][j], ` `" "` `, end` `=` `'') ` ` ` `print` `() ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

**Output:**

Modified matrix is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

Please refer complete article on Program to find transpose of a matrix for more details!

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Program to find transpose of a matrix
- Python Program for Column to Row Transpose using Pandas
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Python program to Convert a Matrix to Sparse Matrix
- Python Program for Program to Print Matrix in Z form
- Program to find the Sum of each Row and each Column of a Matrix
- Program to find all types of Matrix
- Program to find the maximum element in a Matrix
- Program to find Normal and Trace of a matrix
- Python | Transpose elements of two dimensional list
- Program to find the Product of diagonal elements of a matrix
- Python Program for Matrix Chain Multiplication | DP-8
- Python Program for Maximum size square sub-matrix with all 1s
- Python Program for Program to find area of a circle
- Python program to search for the minimum element occurring consecutively n times in a matrix
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python Counter| Find duplicate rows in a binary matrix
- Program to check diagonal matrix and scalar matrix
- C++ program to Convert a Matrix to Sparse Matrix
- Program to convert given Matrix to a Diagonal Matrix