Python Program to Transpose a Matrix – 2024

Hey guys, in this blog we will see a Python Program to Transpose a Matrix.


# Python Program to Transpose a Matrix

X = [[21,9],
    [14 ,3],
    [32 ,1]]

result = [[0,0,0],

# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[j][i] = X[i][j]

for r in result:


[21, 14, 32]
[9, 3, 1]
  • This is the simplest code to transpose a matrix.
  • Suppose the size of the input matrix is mXn.
  • Our result matrix will be nXm.
  • Now we will just traverse in our input matrix. We will name rows iterator as i and columns iterator as j.
  • The we will simply replace the result[j][i] with input[i][j].

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *