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 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

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 vs Deepcopy exercises

All Python exercises · 50 practice problems · Data Structures quiz