easy +10 pts

Transpose 2D Array

Swap rows and columns of a rectangular matrix

Write a function `transpose(matrix)` that takes a rectangular 2D list (list of lists) of integers and returns a new 2D list which is the transpose of the input. The transpose is obtained by swapping rows and columns: the element at row `i` and column `j` of the output is the element at row `j` and column `i` of the input. The input is guaranteed to be rectangular (all rows have the same length). The input matrix is not modified. If the matrix is empty, return an empty list. If the matrix has one row, return a list of single-element lists, each containing that column value.

Constraints

- 0 ≤ number of rows ≤ 100 - 0 ≤ number of columns ≤ 100 - Elements are integers. - The matrix is always rectangular. - Output should be a new list, not a reference to the input.

Example

```python
>>> transpose([[1,2,3],[4,5,6]])
[[1,4],[2,5],[3,6]]
>>> transpose([[1],[2],[3]])
[[1,2,3]]
>>> transpose([])
[]
```
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The number of rows in the result equals the number of columns in the original.
Use a list comprehension over the column indices.
Handle the empty matrix before attempting to access columns.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.