Difference Between Deep and Shallow Copy in Python

1. Introduction

In Python, copying objects can be done in two ways: using a shallow copy or a deep copy. A shallow copy creates a new object, but it doesn't create copies of nested objects; instead, it just copies the references to them. Think of it as copying the contents of a folder, but the documents inside are just shortcuts to the original documents. A deep copy, on the other hand, creates a new object and recursively copies all the nested objects. This is like making an entirely new folder with copies of all the documents and subfolders inside.

2. Key Points

1. Object Duplication: Shallow copy duplicates the top-level container, and deep copy duplicates all levels.

2. Nested Objects: Shallow copy references nested objects, deep copy creates duplicates of nested objects.

3. Memory Usage: Shallow copy is more memory efficient, and deep copy uses more memory.

4. Modifications: Changes to a shallow copy can affect the original, changes to a deep copy are independent.

3. Differences

Aspect Shallow Copy Deep Copy
Object Duplication Duplicates top-level container Duplicates all levels
Nested Objects References nested objects Creates duplicates of nested objects
Memory Usage More memory efficient Uses more memory
Modifications Can affect the original Independent of the original

4. Example

# Example of Shallow Copy
import copy
original_list = [[1, 2, 3], [4, 5, 6]]
shallow_copied_list = copy.copy(original_list)
shallow_copied_list[0][0] = 'X'

# Example of Deep Copy
deep_copied_list = copy.deepcopy(original_list)
deep_copied_list[0][0] = 'Y'

Output:

Shallow Copy Output:
Original: [['X', 2, 3], [4, 5, 6]]
Shallow Copied: [['X', 2, 3], [4, 5, 6]]
Deep Copy Output:
Original: [[1, 2, 3], [4, 5, 6]]
Deep Copied: [['Y', 2, 3], [4, 5, 6]]

Explanation:

1. In the shallow copy example, changing an element in shallow_copied_list also changes the same element in original_list because the nested lists are referenced.

2. In the deep copy example, deep_copied_list is entirely independent of original_list. Changes in the deep copy do not reflect in the original.

5. When to use?

- Use a shallow copy when you want to create a new collection object and you are okay with having references to the original nested objects.

- Use a deep copy when you need a fully independent copy of an entire structure, including all nested objects.

Related Python Posts:

Difference Between Local and Global Variables in Python

Difference Between List and Tuple in Python

Difference Between Array and List in Python

Difference Between List and Dictionary in Python

Difference Between List, Tuple, Set and Dictionary in Python

Difference Between a Set and Dictionary in Python

Difference between for loop and while loop in Python

Difference Between pass and continue in Python

Difference Between List append and extend in Python

Difference Between == and is operator in Python

Difference Between Deep and Shallow Copy in Python

Class Method vs Static Method in Python

Class Method vs Instance Method in Python

Difference Between List and Set in Python

Difference Between Generator and Iterator in Python

Difference Between str and repr in Python

Method Overloading vs Overriding in Python

Difference Between Dictionary and Tuple in Python

Difference Between Dictionary and Object in Python

Difference Between Mutable and Immutable in Python

Difference Between Interface and Abstract Class in Python

Difference Between Python Script and Module

Difference Between for Loop and Iterator in Python

Comments