ProgrammingHardTCS NQT pattern

What is printed?

What is printed?
matrix = [[1, 2, 3], [4, 5, 6]]
print([[row[i] for row in matrix] for i in range(3)])
  1. A. [[1, 2, 3], [4, 5, 6]]
  2. B. [[1, 4], [2, 5], [3, 6]]
  3. C. [[1, 2], [4, 5], [3, 6]]
  4. D. [[1, 4], [2, 5]]

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

This is a transpose: for each column index i, the inner comprehension collects row[i] from every row, giving columns [1,4], [2,5], [3,6].

More Python Programming questions

Exams that ask this topic