To perform a deep copy of a dictionary (dict) in Python, we can make use of the deepcopy() function from the copy module.
Example:
import copy
original_dict = {
'city': 'Chicago',
'temp_forecast_week': [11, 11, 8, 12, 12, 11, 10],
'metadata': {'version': 'v1.0'}
}
# Deep copy example
deep_copied_dict = copy.deepcopy(original_dict)
# Modify the deep copied dict
deep_copied_dict['temp_forecast_week'] = [15, 11, 8, 12, 12, 11, 10]
# Print both dictionaries
print("Original Dict:", original_dict)
print("Deep Copied Dict:", deep_copied_dict)

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!