- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find the transpose of given matrix in Python
Suppose we have a (n by n) matrix M, we have to find its transpose. As we know the transpose of a matrix switches the row and column indices. More formally, for every r and c, matrix[r][c] = matrix[c][r].
So, if the input is like
then the output will be
To solve this, we will follow these steps −
- M := a new list
- tracker := 0
- while tracker < row count of matrix, do
- temp := a new list
- for each row in matrix, do
- temp := join temp and a list with element row[tracker]
- M := M join another list with element temp
- tracker := tracker + 1
- return M
Let us see the following implementation to get better understanding −
class Solution: def solve(self, matrix): M =  tracker = 0 while tracker < len(matrix): temp =  for row in matrix: temp += [row[tracker]] M += [temp] tracker += 1 return M ob = Solution() matrix = [ [7, 2, 6], [3, 7, 2], [5, 3, 7] ] print(ob.solve(matrix))
[[7, 2, 6], [3, 7, 2], [5, 3, 7]]
[[7, 3, 5], [2, 7, 3],[6, 2, 7]]
- Python Program to find the transpose of a matrix
- Find the transpose of a matrix in Python Program
- C++ Program to Find Transpose of a Matrix
- Java Program to Find Transpose of a Matrix
- C++ Program to Find Transpose of a Graph Matrix
- Java program to print the transpose of a matrix
- Transpose a matrix in Python?
- Java program to transpose a matrix.
- Python Program to find out the determinant of a given special matrix
- How to Transpose a Matrix using Python?
- How to calculate transpose of a matrix using C program?
- Transpose a matrix in C#
- Transpose a matrix in Java
- How to Transpose a matrix in Single line in Python?
- Program to find area of largest square of 1s in a given matrix in python