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).