Editor

Make an Independent Copy

Beginner · Fix the bug · 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 lesson

The code below tries to create a backup of the original list before modifying it. But the backup changes too! Fix the code so the backup stays unchanged.

Expected output:

Original: [1, 2, 3, 4]
Backup: [1, 2, 3]
original = [1, 2, 3]
backup = original
original.append(4)
print(f"Original: {original}")
print(f"Backup: {backup}")

More Copy and Deepcopy exercises

All Python exercises · 50 practice problems · Data Structures quiz