Leetcode – Transpose Matrix Solution in Python

Spread the love

Matrix operations are foundational in many areas of computer science, from graphics rendering to scientific computation. The act of transposing a matrix – flipping a matrix over its primary diagonal – is one such operation that’s fundamental in linear algebra. The Transpose Matrix problem on Leetcode provides an opportunity for coders to grapple with this concept in the context of a programming challenge. This article will give an in-depth exploration of the problem, its analytical and coding solutions, as well as complexity analysis.

Problem Statement

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over its primary diagonal, which runs from the top-left to bottom-right.


  • 1 ≤ A.length ≤ 1000
  • 1 ≤ A[0].length ≤ 1000


  1. Input: A = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
  2. Input: A = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]


The matrix transpose operation essentially means converting rows of the original matrix into columns of the new matrix (and vice versa). For example, the first row of the original matrix becomes the first column of the transposed matrix.

The primary diagonal is the set of elements A[i][i] (from the top-left to bottom-right). In the transpose operation, the element at A[i][j] in the original matrix will move to A[j][i] in the transposed matrix.

Solution Strategy

Given the insights from our analysis:

  1. Initialize a new matrix result with a number of rows equal to the number of columns in A and a number of columns equal to the number of rows in A.
  2. Iterate over the rows and columns of matrix A.
  3. For each cell in A, set its value in the corresponding cell in the result matrix. Specifically, the value at A[i][j] is placed in result[j][i].

Python Code:

def transpose(A: List[List[int]]) -> List[List[int]]:
    # Determine the dimensions of the input matrix
    R, C = len(A), len(A[0])
    # Initialize the transposed matrix with empty values
    result = [[None] * R for _ in range(C)]
    # Populate the transposed matrix
    for r in range(R):
        for c in range(C):
            result[c][r] = A[r][c]
    return result

Complexity Analysis

  1. Time Complexity: The above solution iterates through each cell of the matrix exactly once, leading to a time complexity of O(R*C), where R is the number of rows and C is the number of columns in the matrix A.
  2. Space Complexity: We are creating a new matrix to store the result, which has R rows and C columns, leading to a space complexity of O(R*C). This is optimal as we need to store the result in a separate matrix since we cannot perform this operation in-place (when the number of rows and columns are not the same).

Alternate Method: Pythonic One-liner

Python’s list comprehension can be leveraged to solve this problem in a more concise manner:

def transpose(A: List[List[int]]) -> List[List[int]]:
    return [list(row) for row in zip(*A)]

The zip(*A) function essentially unzips the rows of A into individual columns, and the list comprehension converts these columns back into lists.


The Transpose Matrix problem is a neat introduction to basic matrix operations in a computational setting. While the core algorithmic challenge might not be as complex as some other problems, the problem underscores the importance of understanding basic mathematical operations and how to translate them into efficient code. The problem also serves as a bridge, linking foundational mathematical concepts with their practical applications in programming and algorithm design.

Leave a Reply