Editor

Predict the Output: Shallow Copy Trap

Intermediate · Predict the output · from Python Copy vs Deepcopy: Why Your "Copy" Isn't Really a Copy

From the lesson: Python Copy vs Deepcopy: Why Your "Copy" Isn't Really a Copy

Imagine you have a folder full of documents. You photocopy the folder and give it to a friend. But here's the catch - the photos inside the folder aren't copies. They're the same physical photos. If your friend draws a mustache on one, your original photo is ruined too.

Read the lesson

This code uses a shallow copy on nested data. Read it carefully and predict what will be printed.

Type the exact output.

original = [[1, 2], [3, 4]]
shallow = original.copy()
shallow[0].append(5)
print(original)
print(shallow)

More Copy vs Deepcopy exercises

All Python exercises · 50 practice problems · Data Structures quiz