v9.1.1
Menu

Transpose Array

easyFREEArray ManipulationNumPy Array ManipulationNumpy

Problem Statement

Transpose a 2D NumPy array (swap rows and columns).

Background

Transposing swaps rows and columns of a matrix. For an array of shape (m, n), transpose gives shape (n, m).

Your Task

Write a function transpose_array(arr) that:

  1. Transposes the input 2D array
  2. Returns a dictionary with:
    • "original_shape": Original shape as list
    • "transposed_shape": New shape as list
    • "array": Transposed array as nested list

Output Format

Return a dictionary with exactly these three keys.

Example

Input
[[1, 2, 3], [4, 5, 6]]
Output
{'original_shape': [2, 3], 'transposed_shape': [3, 2], 'array': [[1, 4], [2, 5], [3, 6]]}
Reasoning
  • Input: arr is a 2×32 \times 3 matrix AA — 2 rows, 3 columns: row 0 is (1,2,3)(1, 2, 3) and row 1 is (4,5,6)(4, 5, 6).
  • The math: the transpose ATA^T is defined by (AT)ij=Aji(A^T)_{ij} = A_{ji}, so every row of AA becomes a column of ATA^T. Row 0 (1,2,3)(1, 2, 3) turns into column 0, and row 1 (4,5,6)(4, 5, 6) turns into column 1.
  • Reading ATA^T row by row: row ii holds the ii-th entry of each original row, giving (1,4)(1, 4), (2,5)(2, 5), (3,6)(3, 6). For example (AT)20=A02=3(A^T)_{20} = A_{02} = 3.
  • Shapes: an m×nm \times n matrix becomes n×mn \times m, so original_shape is [2, 3] and transposed_shape is [3, 2].
  • Output: a dictionary whose array is [[1, 4], [2, 5], [3, 6]], with shapes and values all given as plain lists rather than array objects.
  • Notice the entries on the main diagonal (A00=1A_{00} = 1, A11=5A_{11} = 5) stay where they are; transposing twice returns the original matrix.

Constraints

  • Use .T attribute or np.transpose()
  • Input will always be 2D
  • Return shapes as lists

Learn this in

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.