Data types · 27 / 29 Exercise Hard

Shallow versus deep copy

Objective

With nested lists, a shallow copy shares the inner lists with the original; a deep copy duplicates them too.

Instructions

  • Create original = [[1, 2], [3, 4]], one copy with copy.copy and another with copy.deepcopy.
  • Then change original[0][0] to 99.
  • Show Shallow: ... and Deep: ..., each on its own line, with the resulting list.
Output
Shallow: [[99, 2], [3, 4]]
Deep: [[1, 2], [3, 4]]

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise