Transpose of a Matrix

KoshurAI
Jan 8, 2023

--

In Python, you can transpose a matrix using the built-in zip() function. Here's an example:

matrix = [[1, 2, 3], [4, 5, 6]]
transposed_matrix = list(map(list, zip(*matrix)))
print(transposed_matrix) # [[1, 4], [2, 5], [3, 6]]

Alternatively, you can use a list comprehension to achieve the same result:

matrix = [[1, 2, 3], [4, 5, 6]]
transposed_matrix = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
print(transposed_matrix) # [[1, 4], [2, 5], [3, 6]]

Both of these approaches will work for any rectangular matrix (i.e., a matrix where all rows have the same number of columns).

--

--

KoshurAI
KoshurAI

Written by KoshurAI

Passionate about Data Science? I offer personalized data science training and mentorship. Join my course today to unlock your true potential in Data Science.

No responses yet