← Back to list

Shallow vs. Deep Copy in Python: Which One Should You Use?

Imagine you have a list of data, and you need to make a copy of it. You want to ensure the changes made on the copied list will not affect…

Maryem TAYEBI · 2025-03-02 07:13 · 0 claps · 4.4 min read
#python #shallow-copy #deep-copy #memory-improvement #memory-management
Open on Medium ↗
Wiki topics: BIZ · Business Strategy

Shallow vs. Deep Copy in Python: Which One Should You Use?

Imagine you have a list of data, and you need to make a copy of it. You want to ensure the changes made on the copied list will not affect the original list. In that case, which copy would you choose: shallow or deep?

#which one choose : shallow or deep
import copy
original = [1, 2, 3]
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)

To answer this question, let’s first understand shallow copy and deep copy and explore the characteristics of each.

What’s shallow copy?

According to its definition, a shallow copy constructs a new compound object and then inserts references into it, linking to the child objects found in the original. The copying process does not go deeper into nested structures to create copies of the inner objects, instead, it refers to the same child objects as the original.

To understand how the shallow copy process works, let’s examine the code output and see how it affects memory.

The code demonstrates that the variable a is a list containing other lists, and b is a variable that holds the shallow copy of a. After creating the shallow copy using the copy function, the addresses of both a and b are printed, followed by the addresses of the first child element in each.

Lists in Python are mutable, meaning their contents can be changed without changing their reference in memory. When a shallow copy is created, a new outer list is allocated in memory for b, which is why a and b have different addresses. However, instead of creating new copies of the inner element, the shallow copy only copies the reference to the original child objects. As a result, the child element in both a and b still point to the same memory locations. This explains why modifying a child element through one list affects the other; the inner objects are shared between both lists.

Below is a schematic diagram of the memory state before and after the shallow copy, providing a clearer view of how the memory manages the shallow copy process as applied in our code

What is the deep copy?

According to its definition, a deep copy creates a completely new object and recursively duplicates all its elements. This means that every object inside the original is copied rather than just referenced. As a result, the new object and all its nested elements are entirely separate from the original, ensuring that changes made to one do not affect the other.

To understand how the deep copy process works, let’s modify the shallow copy example to perform the deep copy instead. Observe the output of the code below to see how it affects memory.

Since we are using a list, which is a mutable object, the behavior of the outer list in a deep copy is the same as in a shallow copy; a and b have different addresses because b is a newly created list in memory. However, unlike a shallow copy, where the child elements still reference the same objects, a deep copy recursively creates new copies of all child objects. As a result, the addresses of the child elements in a and b are also different. This ensures that modifications made to b do not affect a, and vice versa, making deep copying essential when full independence of a mutable object is required

Below is a schematic diagram of the memory state before and after the deep copy, providing a clearer view of how the memory manages the deep copy process as applied in our code

Use Cases Examples for Shallow and Deep Copy

Shallow Copy Use Case

A warehouse system is required to sort inventory items by price for a promotional discount temporarily. The original order must remain unchanged, but the discount should apply to the same product references.

The inventory is stored as a list of dictionaries containing products and their prices. A shallow copy allows us to sort the list without affecting the original structure while referencing the same products.

Deep Copy Use Case

A university student management system generates a completely independent copy of student records for backup. Changes made to the copied records should not impact the original data.

The student records are stored as a list of dictionaries containing personal details and courses. We created a deep copy to ensure that any changes to the backup do not modify the original records.

Summarize

The table below summarizes the key differences between shallow and deep copy

Conclusion

Now that you understand the difference between shallow and deep copy, you can answer our initial question. Both methods create a copy of the list; however, to ensure that changes made to the copied list do not affect the original, the best choice is a deep copy. It guarantees that modifications in the original do not impact the copy and vice versa.

Do you agree with this conclusion, or do you have a different answer?


메타데이터
post_id
5fbd4ea7a8ba
slug
shallow-vs-deep-copy-in-python-which-one-should-you-use-5fbd4ea7a8ba
url
https://medium.com/@tmaryem/shallow-vs-deep-copy-in-python-which-one-should-you-use-5fbd4ea7a8ba
canonical_url
https://medium.com/@tmaryem/shallow-vs-deep-copy-in-python-which-one-should-you-use-5fbd4ea7a8ba
author_url
https://medium.com/@tmaryem
status
ok
fetched_at
2026-06-16 19:09:56