Python Deep Copy and Shallow Copy: Understanding the Difference

Python, a versatile and widely-used programming language, offers several ways to work with objects and data structures. When dealing with complex data structures like lists, dictionaries, or custom objects, it’s crucial to understand the concepts of deep copy and shallow copy. These concepts are essential for maintaining data integrity and preventing unintended side effects when manipulating objects. In this article, we’ll explore deep copy and shallow copy in Python, their differences, and provide examples to illustrate their usage.

1. What is Copying?

  1. Before diving into deep and shallow copies, let’s clarify what copying means in Python.
  2. When you create a copy of an object, you essentially create a new object with the same data. This allows you to modify one copy without affecting the other.
  3. Python provides two primary methods for copying objects: deep copy and shallow copy.

2. Shallow Copy.

  1. A shallow copy creates a new object, but it doesn’t create copies of the objects contained within the original object. Instead, it copies references to these objects.
  2. This means that changes made to the objects inside the copied container will affect both the original and the copied object if those objects are mutable.
  3. You can create a shallow copy in Python using the `copy` module’s `copy()` function or the object’s built-in method `copy()` for some types like lists.
  4. Example source code.
    import copy
    
    def shallow_copy():
    
        # Using the copy() function from the copy module
        original_list = [[1, 2, 3], [4, 5, 6]]
        print('original_list: ', original_list)
        shallow_copied_list = copy.copy(original_list)
        print('shallow_copied_list: ', shallow_copied_list)
    
        # Modifying the shallow copied list affects the original
        shallow_copied_list[0][0] = 100
        print('original_list: ', original_list)    # Output: [[100, 2, 3], [4, 5, 6]]
    
        # Using the built-in copy() method for lists
        original_list = [[1, 2, 3], [4, 5, 6]]
        shallow_copied_list = original_list.copy()
    
        # Modifying the shallow copied list affects the original
        shallow_copied_list[0][1] = 100
        print('original_list: ', original_list) # Output: [[1, 100, 3], [4, 5, 6]]
    
    if __name__ == "__main__":
    
        shallow_copy()
  5. Output.
    original_list:  [[1, 2, 3], [4, 5, 6]]
    shallow_copied_list:  [[1, 2, 3], [4, 5, 6]]
    original_list:  [[100, 2, 3], [4, 5, 6]]
    original_list:  [[1, 100, 3], [4, 5, 6]]
  6. As seen in the examples above, modifying the elements within the shallow copied list also alters the original list.

3. Deep Copy.

  1. A deep copy, on the other hand, creates entirely independent copies of both the original object and all the objects contained within it.
  2. This means that changes made to the objects inside the copied container won’t affect the original object.
  3. You can create a deep copy in Python using the `copy` module’s `deepcopy()` function.

    import copy
    
    def deep_copy():
    
        # Using the deepcopy() function from the copy module
        original_list = [[1, 2, 3], [4, 5, 6]]
        deep_copied_list = copy.deepcopy(original_list)
    
        # Modifying the deep copied list doesn't affect the original
        deep_copied_list[0][0] = 100
        print('original_list: ', original_list)          # Output: [[1, 2, 3], [4, 5, 6]]
        print('deep_copied_list: ', deep_copied_list)          # Output: [[1, 2, 3], [4, 5, 6]]
    
    
    if __name__ == "__main__":
    
        deep_copy()
  4. Output.
    original_list:  [[1, 2, 3], [4, 5, 6]]
    deep_copied_list:  [[100, 2, 3], [4, 5, 6]]
  5. In this example, changing the elements within the deep copied list has no impact on the original list.

4. When to Use Shallow Copy vs. Deep Copy.

  1. The choice between shallow and deep copying depends on your specific use case:
  2. Use shallow copy when you want to create a new object that contains references to the same objects as the original. This is useful when you want to duplicate the structure but not the content of nested objects.
  3. Use deep copy when you need a completely independent copy of the original object, including all nested objects. Deep copying ensures that changes to the copied object won’t affect the original.

5. Conclusion.

  1. Understanding the concepts of deep copy and shallow copy is essential for effective data manipulation in Python.
  2. Shallow copy creates a new object with references to the same internal objects, while deep copy creates a fully independent copy, including all nested objects.
  3. Choosing the appropriate copy method depends on your specific requirements for data integrity and manipulation.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.