ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
grid = [[1, 2], [3, 4]]
flat = [v for row in grid for v in row]
print(flat, sum(flat) == sum(map(sum, grid)))
  1. A. [1, 3, 2, 4] True
  2. B. [1, 2, 3, 4] True
  3. C. [1, 2, 3, 4] False
  4. D. [[1, 2], [3, 4]] 10

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

The nested comprehension reads row by row, so the order is 1, 2, 3, 4. Both ways of summing the grid give 10.

More Python Programming questions

Exams that ask this topic