Problem 234196 · easy · Phase 02 Linear Data Structures

Transpose a Rectangular Grid

2d-lists · transpose · list-comprehension

Given a rectangular grid grid with R rows and C columns, return a new grid with C rows and R columns such that the value at row r, column c of the input appears at row c, column r of the output.

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6]]
Output: [[1, 4],
         [2, 5],
         [3, 6]]

Input:  grid = [[7]]
Output: [[7]]

Constraints

  • 1 <= rows, cols <= 100
  • Return a new grid; do not modify the input.

Goals

  • Swap the roles of rows and columns
  • Handle non-square grids where the output shape differs from the input
Starting Python…