Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Transpose a 2D list

Write a transpose function for 2D lists that handles regular and irregular rows by filling missing entries with None.

Python practice15 minList & Dictionary PatternsIntermediateLast updated March 20, 2026

Problem statement

Given a 2D list (a list of rows), return its transpose (columns become rows). Rows may have different lengths; when a row is missing a value for a transposed column, the corresponding position should be filled with None. An empty input should return an empty list. Examples: - [[1,2,3],[4,5,6]] -> [[1,4],[2,5],[3,6]] - [[1,2],[3]] -> [[1,3],[2,None]]

Task

Implement a transpose function that returns the matrix transpose. If rows have unequal lengths, missing elements should be represented by None in the transposed result.

Examples

Square-ish matrix

Input

transpose([[1,2,3],[4,5,6]])

Output

[[1, 4], [2, 5], [3, 6]]

Standard transpose: columns become rows.

Input format

A list of lists representing rows (matrix).

Output format

A list of lists representing the transposed matrix; missing values padded with None.

Constraints

- Use only built-in Python modules. - Aim for O(r * c) where r = number of rows and c = length of the longest row.

Samples

Sample 1

Input

transpose([[1,2],[3]])

Output

[[1, 3], [2, None]]

Second row is shorter, so the missing entry in the second column is None.