ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE

What does the following Python 3 code print? matrix = [[1, 2], [3, 4], [5, 6]] transposed = [[row[i] for row in matrix] for i in range(2)] print(transposed)

  1. A. [[1, 3], [2, 4], [5, 6]]
  2. B. [[1, 2], [3, 4], [5, 6]]
  3. C. [[1, 3, 5], [2, 4, 6]]
  4. D. [[1, 2, 3], [4, 5, 6]]

Correct answer: C. [[1, 3, 5], [2, 4, 6]]

For each column index i, the outer comprehension gathers that column across all rows, producing the transpose.

More Python Programming questions

Exams that ask this topic