ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE

What does the following Python 3 code print? a = [[0] * 2] * 2 a[0][0] = 7 print(a)

  1. A. [[7, 0], [0, 0]]
  2. B. [7, 0]
  3. C. [[7, 0], [7, 0]]
  4. D. [7, [0, 0]]

Correct answer: C. [[7, 0], [7, 0]]

[row] * m repeats references to the same inner list, so writing through one row shows in all rows. Output: [[7, 0], [7, 0]].

More Python Programming questions

Exams that ask this topic