0
0
PythonProgramBeginner · 2 min read

Python Program to Transpose a Matrix

To transpose a matrix in Python, you can use transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))] which flips rows and columns.
📋

Examples

Input[[1, 2], [3, 4]]
Output[[1, 3], [2, 4]]
Input[[5, 6, 7], [8, 9, 10]]
Output[[5, 8], [6, 9], [7, 10]]
Input[[1]]
Output[[1]]
🧠

How to Think About It

To transpose a matrix, think of swapping its rows and columns. The first row becomes the first column, the second row becomes the second column, and so on. You pick elements column-wise from each row to build the new rows.
📐

Algorithm

1
Get the input matrix.
2
Create a new matrix with the number of rows equal to the original matrix's column count.
3
For each column index in the original matrix, collect elements from each row at that column index.
4
Add this collection as a new row in the transposed matrix.
5
Return or print the transposed matrix.
💻

Code

python
matrix = [[1, 2], [3, 4]]
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
print(transposed)
Output
[[1, 3], [2, 4]]
🔍

Dry Run

Let's trace the matrix [[1, 2], [3, 4]] through the code.

1

Original matrix

[[1, 2], [3, 4]]

2

Number of columns

len(matrix[0]) = 2

3

First iteration (i=0)

Collect elements at index 0 from each row: [1, 3]

4

Second iteration (i=1)

Collect elements at index 1 from each row: [2, 4]

5

Final transposed matrix

[[1, 3], [2, 4]]

iCollected elements
0[1, 3]
1[2, 4]
💡

Why This Works

Step 1: Access columns by index

We use range(len(matrix[0])) to loop over column indexes because the number of columns is the length of the first row.

Step 2: Collect elements from each row

For each column index, we pick the element at that index from every row using row[i].

Step 3: Build new rows

Each collection of elements from the same column forms a new row in the transposed matrix.

🔄

Alternative Approaches

Using zip with unpacking
python
matrix = [[1, 2], [3, 4]]
transposed = list(map(list, zip(*matrix)))
print(transposed)
This method is concise and uses Python's built-in functions but may be less clear to beginners.
Using nested loops
python
matrix = [[1, 2], [3, 4]]
transposed = []
for i in range(len(matrix[0])):
    new_row = []
    for row in matrix:
        new_row.append(row[i])
    transposed.append(new_row)
print(transposed)
This approach is more verbose but easier to understand step-by-step.

Complexity: O(m*n) time, O(m*n) space

Time Complexity

The program visits each element once to rearrange it, so the time grows with the number of elements (rows * columns).

Space Complexity

A new matrix of the same size is created to hold the transposed elements, so space also grows with the number of elements.

Which Approach is Fastest?

Using zip(*matrix) is usually fastest and most memory efficient in Python due to internal optimizations.

ApproachTimeSpaceBest For
List comprehensionO(m*n)O(m*n)Clear and Pythonic for beginners
zip with unpackingO(m*n)O(m*n)Concise and efficient for Python users
Nested loopsO(m*n)O(m*n)Step-by-step clarity for learning
💡
Use zip(*matrix) for a quick and Pythonic transpose.
⚠️
Trying to transpose without considering that all rows must have the same length, which can cause errors.