What is the difference between a shallow copy and a deep copy in Python?
Antek N
antek n profile pic

In Python, the concepts of shallow copy and deep copy refer to different ways of creating copies of objects. Here's a detailed explanation of the differences between shallow copy and deep copy: Shallow Copy: A shallow copy creates a new object that is a copy of the original object, but the elements within the object are still references to the same objects as the original. In other words, it creates a new object with references to the original objects. Changes made to the original objects will be reflected in the copied object and vice versa.

1
2
3
4
5
6
7
8
9
10
11
12

import copy

original_list = [1, 2, [3, 4]]

shallow_copy_list = copy.copy(original_list)

# Modify the nested list in the original list
original_list[2][0] = 5

print(original_list)    # Output: [1, 2, [5, 4]]
print(shallow_copy_list)# Output: [1, 2, [5, 4]]

In the example above, thecopy.copy() function is used to create a shallow copy of theoriginal_list. Both theoriginal_list andshallow_copy_list initially contain the same elements. However, modifying the nested list in theoriginal_list also affects the nested list in theshallow_copy_list. Deep Copy: A deep copy creates a new object that is a complete and independent copy of the original object, including all nested objects. It recursively copies the objects, creating separate copies at all levels. Changes made to the original objects will not affect the copied object, and vice versa.

1
2
3
4
5
6
7
8
9
10
11
12

import copy

original_list = [1, 2, [3, 4]]

deep_copy_list = copy.deepcopy(original_list)

# Modify the nested list in the original list
original_list[2][0] = 5

print(original_list)    # Output: [1, 2, [5, 4]]
print(deep_copy_list)   # Output: [1, 2, [3, 4]]

In this example, thecopy.deepcopy() function is used to create a deep copy of theoriginal_list. Modifying the nested list in theoriginal_list does not affect the nested list in thedeep_copy_list, as they are now independent copies. In summary, the main difference between shallow copy and deep copy lies in how they handle nested objects. A shallow copy creates a new object with references to the original nested objects, while a deep copy creates completely independent copies of all objects, including nested objects. When working with nested data structures, consider the desired behavior and use either shallow copy or deep copy accordingly.

Similar Questions

What is the difference between a shallow copy and a deep copy of a set in Python?

What is the difference between a shallow copy and a deep copy of a list in Python?

What is the difference between a shallow copy and a deep copy of a tuple in Python?

What is the difference between a deep copy and a shallow copy of an object in Python?

What is the difference between a shallow copy and a deep copy of a dictionary in Python?

What is the difference between a shallow copy and a deep copy of a nested set in Python?

What is the difference between a shallow copy and a deep copy of a nested list in Python?

What is the difference between a shallow copy and a deep copy of a nested tuple in Python?

What is the difference between a deep copy and a shallow copy of a nested dictionary in Python?

What is the difference between a deep copy and a shallow copy of a nested set with mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested list with mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested tuple with mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested dictionary with mutable values in Python?

What is the difference between a deep copy and a shallow copy of a nested set with nested mutable elements in Python?

What is the difference between a deep copy and a shallow copy of a nested set with nested mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested list with nested mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested list with nested mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested tuple with nested mutable elements in Python?

What is the difference between a shallow copy and a deep copy of a nested tuple with nested mutable elements in Python?

What is the difference between a decorator and a closure in Python?