Shallow Copy and Deep Copy in Python

In Python, the concepts of shallow copy and deep copy are used when working with complex data structures like lists and dictionaries. Understanding these concepts is important to avoid unintended side effects when modifying data

  1. Shallow copy: A shallow copy creates a new object that references the same data as the original object. This means that if you make changes to the new object, the original object will also be changed.
  2. Deep copy: A deep copy creates a new object that contains copies of the data in the original object. This means that if you make changes to the new object, the original object will not be changed.

Shallow Copy in Python

A shallow copy creates a new object that is a copy of the original container (list, dictionary, etc.), but it only copies the top-level elements of the container. If the container contains nested objects (e.g., lists inside a list), the copy will reference the same nested objects instead of creating independent copies.

import copy # Original list with nested lists original_list = [[1, 2, 3], [4, 5, 6]] # Shallow copy shallow_copied_list = copy.copy(original_list) # Modify the shallow copied list shallow_copied_list[0][0] = 100 print(original_list) # Output: [[100, 2, 3], [4, 5, 6]] print(shallow_copied_list) # Output: [[100, 2, 3], [4, 5, 6]]

As you can see, when we modified the first element of the shallow copied list, it also affected the original list because they share the same nested objects.

Deep Copy in Python

A deep copy creates a new object that is a completely independent copy of the original container, including all the nested objects. The nested objects are recursively copied, ensuring that changes made to the deep copy do not affect the original data.

import copy # Original list with nested lists original_list = [[1, 2, 3], [4, 5, 6]] # Deep copy deep_copied_list = copy.deepcopy(original_list) # Modify the deep copied list deep_copied_list[0][0] = 100 print(original_list) # Output: [[1, 2, 3], [4, 5, 6]] print(deep_copied_list) # Output: [[100, 2, 3], [4, 5, 6]]

In this case, modifying the first element of the deep copied list did not affect the original list because they are completely independent copies.

Python Copy module

The copy module in Python provides functions for performing shallow and deep copies. The copy.copy() function performs a shallow copy, and the copy.deepcopy() function performs a deep copy.

Here is an example of how to use the copy module to perform a shallow copy and a deep copy:

import copy list1 = [1, 2, 3, [4, 5, 6]] list2 = copy.copy(list1) list3 = copy.deepcopy(list1) print("list1:", list1) print("list2:", list2) print("list3:", list3) list1[0] = 10 list1[3][0] = 100 print("list1:", list1) print("list2:", list2) print("list3:", list3)

The output of the code is as follows:

list1: [1, 2, 3, [4, 5, 6]] list2: [1, 2, 3, [4, 5, 6]] list3: [1, 2, 3, [4, 5, 6]] list1: [10, 2, 3, [100, 5, 6]] list2: [10, 2, 3, [100, 5, 6]] list3: [1, 2, 3, [4, 5, 6]]

Difference between Deep Copy and Shallow Copy in Python

The main difference between deep copy and shallow copy in Python lies in how they handle nested data structures. Both types of copies are used to create copies of complex data structures, such as lists or dictionaries, but they behave differently when it comes to nested objects.

Conclusion

  1. Shallow copy creates a new object that copies the top-level elements but shares references to nested objects with the original.
  2. Deep copy creates a new object that is a fully independent copy, including all the nested objects.

When using nested data structures, especially in scenarios where you need to modify data independently, it is essential to choose between shallow copy and deep copy based on your specific needs.