Matrix Transpose Using Nested Loops

Hard ⏱ 18 min 33% acceptance ★★★★☆ 4.3
Write a function transpose(matrix) that returns the transpose of a rectangular 2D list matrix (rows become columns), computed with nested for loops (do not use zip(*matrix)).

Examples

Example 1
Input
transpose([[1, 2, 3], [4, 5, 6]])
Output
[[1, 4], [2, 5], [3, 6]]
Explanation

Row/column indices swap.

Constraints

  • matrix is a non-empty rectangular list of lists.
  • Must not use zip(*matrix).

Topics

Loopsnested loops

Companies

MicrosoftAdobe

Hints

Hint 1

If the original has R rows and C columns, the transpose has C rows and R columns.

Hint 2

For each column index j, build a new row by collecting matrix[i][j] across all i.

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