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 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 lessonCreate 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 vs Deepcopy exercises
- Predict the Output: Assignment AliasingBeginner
- Make an Independent CopyBeginner
- Predict the Output: Shallow Copy TrapIntermediate
All Python exercises · 50 practice problems · Data Structures quiz