Transpose a Matrix

Medium ⏱ 12 min 61% acceptance ★★★★★ 4.6
Write transpose(matrix) that returns the transpose of a rectangular 2D list matrix (rows become columns). Assume all rows have the same length.

Examples

Example 1
Input
matrix = [[1, 2, 3], [4, 5, 6]]
Output
[[1, 4], [2, 5], [3, 6]]
Explanation

Row i, column j moves to row j, column i.

Constraints

  • 1 <= rows, cols <= 200

Topics

ListsMatrixNested Lists

Companies

GoogleIntel

Hints

Hint 1

zip(*matrix) transposes rows into columns directly.

Hint 2

Convert each resulting tuple back to a list.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.