A shallow copy in Python creates a new object, but it only copies references to the original nested objects. Changes to nested elements in the copied object will affect the original.
Deep copy in Python is a process where each object or nested object gets copied entirely. In simple words, this means that any changes made to the deep copy will not reflect on the original one as they have saved separately at different memory locations. The deepcopy()
function of Python's copy module can be used in order to carry out deep copying.
There are two main ways to copy objects in Python:
Shallow copy: Only copies the reference of nested objects.
Deep copy: Copies the entire object, including nested ones, to create an independent copy.
To show the difference between shallow and deep copy, consider a list of lists in Python. A shallow copy would create a new object but still reference the nested objects. However, A deep copy creates entirely new copies of the nested objects, preventing any modification from affecting the original object.
In this Answer, we’ll focus on Python deep copy, explaining its definition and syntax and providing examples to understand how it works.
In Python, a deep copy ensures that a new object is created for each object in the original. This is particularly useful when the object you’re copying contains other objects, such as lists or dictionaries. By using deep copy, you can ensure that modifying the copied object won’t impact the original object or any of its nested elements.
The deepcopy()
function is part of the copy
module in Python. Here is its syntax:
import copydeep_copy_object = copy.deepcopy(object_to_copy)
object_to_copy
: This is the object you want to deep copy.
deep_copy_object
: The newly created object that is a deep copy of the original.
Here’s another example of how Python deep copy works when copying objects that contain nested objects like dictionaries.
import copy# Original dictionary with nested dictionariesoriginal_dict = {'name': 'Alice','grades': {'math': 90,'science': 85,'history': {'midterm': 88, 'final': 92}}}# Perform a deep copydeep_copy_dict = copy.deepcopy(original_dict)# Modify the deep copied dictionarydeep_copy_dict['grades']['math'] = 95deep_copy_dict['grades']['history']['midterm'] = 93# Print results to show the effect of changesprint("Original Dictionary:", original_dict)print("Deep Copied Dictionary:", deep_copy_dict)
In this example, the changes to the deep_copy_dict
do not affect the original_dict
. The deep copy ensures that both dictionaries are stored in different memory locations.
Tip for learners: Try adding more nested levels or modify the original dictionary after copying. For example, change original_dict['grades']['science'] = 88
and see if it impacts deep_copy_dict
. This experiment shows how deep copies behave independently from the original.
Deep copying uses more memory and takes more time than shallow copying. This difference can matter when working with large or complex data and when deep copying isn’t necessary.
For data that won’t be changed (immutable data), a shallow copy is usually faster and more efficient since it doesn’t duplicate data. However, for data that can change (mutable objects), deep copying is safer because it keeps changes to the copy from affecting the original data.
Key Takeaways:
Deep copy definition: In Python, deep copy creates an entirely new object, including all nested objects, ensuring that changes to the copied object do not affect the original.
Difference between shallow and deep copy: A shallow copy copies the reference of nested objects, while a deep copy creates independent copies of both the outer and inner objects.
How to perform deep copy: Python’s deepcopy()
function, available in the copy
module, allows you to create a deep copy of any object, ensuring separate memory allocation.
Ready to deepen your Python knowledge? Start our “Become a Python Developer” path, beginning with Python’s basic concepts and advancing to more complex topics, preparing you for a career as a Python developer.
Haven’t found what you were looking for? Contact Us
Free Resources