Editor

Safe Copy of Nested Data

Intermediate · Write the code · 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

Create a fully independent copy of the original data using deep copy. Then add the score 100 to the copy's scores list. Print the original scores and the copy's scores on separate lines to prove they are independent.

original = {
'name': 'Alice',
'scores': [85, 90, 78],
}
# Create a deep copy
# Add 100 to the copy's scores
# Print original scores, then copy's scores

More Copy and Deepcopy exercises

All Python exercises · 50 practice problems · Data Structures quiz