ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
m = [[0] * 2 for _ in range(2)]
n = m.copy()
n[0][0] = 5
n[1] = [6, 6]
print(m)
  1. A. [[0, 0], [0, 0]]
  2. B. [[5, 0], [6, 6]]
  3. C. [[5, 5], [0, 0]]
  4. D. [[5, 0], [0, 0]]

Correct answer: D. [[5, 0], [0, 0]]

list.copy() is shallow: n[0] is the same inner list as m[0], so n[0][0] = 5 shows in m. Rebinding n[1] to a new list only changes n's slot, not m.

More Python Programming questions

Exams that ask this topic