Transpose a matrix in Python

Source code :

X = [[2,7],
    [4 ,5],]

res = [[0,0],
         [0,0]]
for i in range(len(X)):
   for j in range(len(X):
       res[j][i] = X[i][j]
for r in res:
   print(r)

Output :

[2, 4]
[7, 5]

Algorithm :

  1. Declare two matrix one with values and other to store the result
  2. Use 2 for loops to define rows and columns
  3. Replace row with columns and store them in the result variable
  4. Finally print the result

Output :


                

Comments :