Transpose Array
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:
- Transposes the input 2D array
- 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:
[[1, 2, 3], [4, 5, 6]]
{'original_shape': [2, 3], 'transposed_shape': [3, 2], 'array': [[1, 4], [2, 5], [3, 6]]}2×3 matrix becomes 3x2 after transpose
Constraints:
- Use .T attribute or np.transpose()
- Input will always be 2D
- Return shapes as lists
1. Background Knowledge
NumPy arrays are the foundational data structure for numerical computing in Python, enabling efficient vectorized operations on multi-dimensional data. A 2D array (matrix) of shape (m, n) has m rows and n columns. The transpose operation swaps these dimensions, converting shape (m, n) to (n, m) where element at position (i, j) moves to (j, i).
Mathematically, for matrix A, the transpose AT satisfies: (AT)j,i​=Ai,j​
NumPy provides two primary methods: .T attribute (simple view, no copy for 2D) and np.transpose() (more flexible for >2D).
Prerequisites:
- Basic NumPy: np.array(), .shape
- Array views vs. copies
- Dictionary manipulation in Python
2. Algorithm Approach
Direct transposition using NumPy's optimized C-level implementation:
- Access array shape: arr.shape
- Compute transpose: arr.T or np.transpose(arr)
- Convert to nested list: .tolist() (required by output format)
NumPy's transpose creates a view (zero-copy for 2D arrays), making it O(1) time and space for the operation itself.
Why not manual loops? NumPy's vectorization is 100-1000x faster than Python loops due to C implementation and cache optimization.
3. Step-by-Step Strategy
def transpose_array(arr):
# Step 1: Capture original shape as list
original_shape = list(arr.shape)
# Step 2: Transpose using.T (or np.transpose(arr))
transposed = arr.T
# Step 3: Convert to nested list (required format)
transposed_list = transposed.tolist()
# Step 4: Capture new shape
transposed_shape = list(transposed.shape)
# Step 5: Return dictionary
return {
"original_shape": original_shape,
"transposed_shape": transposed_shape,
"array": transposed_list
}
Verification with sample:
Input: [[1,2,3], [4,5,6]] # shape (2,3)
Output: [[1,4], [2,5], [3,6]] # shape (3,2)
4. Common Pitfalls
- Returning NumPy array instead of nested list: Use .tolist()—problem explicitly requires "array": nested list.
- Shape as tuple: Convert with list(arr.shape)—requires lists.
- Using .transpose() method: Correct is .T attribute or np.transpose().
- Modifying original: .T creates view; use arr.copy().T if mutation avoidance needed (not required here).
- Non-2D input: Constraints guarantee 2D, but .T handles higher-D safely.
- Order of keys: Dictionary order preserved in Python 3.7+ matches requirement.
| Mistake | Fix |
|---|---|
| return {"array": arr.T} | arr.T.tolist() |
| "original_shape": arr.shape | list(arr.shape) |
| Manual loops | Use arr.T |
5. Time & Space Complexity
Time Complexity: O(mn)
- .T operation: O(1) (creates metadata view)
- .tolist(): O(mn) (copies all elements to Python lists)
- Shape operations: O(1)
Space Complexity: O(mn)
- Output nested list: stores full copy of transposed data
- Dictionary overhead: O(1)
- NumPy view: O(1) extra (not counted in final output)
Total: O(mn) time and space, where m,n are original dimensions. Optimal—no better asymptotic possible for full materialization.
Edge Cases Handled:
- Square matrices: (n,n) → (n,n)
- 1-row/1-col: (1,n) → (n,1) ✓
- Empty: Constraints imply non-empty 2D.