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 photos. You make a copy of the folder and give it to a friend. But here's the catch - only the folder is new. The photos inside are the same physical photos, shared by both folders. If your friend draws a mustache on one, your photo has the mustache too.
Read the lessonThis 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 and Deepcopy exercises
- Predict the Output: Assignment AliasingBeginner
- Make an Independent CopyBeginner
- Safe Copy of Nested DataIntermediate
- Fix the Game State BugAdvanced
All Python exercises · 50 practice problems · Data Structures quiz