Shallow and Deep Copy Concepts
Shallow and deep copies are important concepts when working with NumPy arrays and Python objects. They help you understand whether data is shared between objects or duplicated independently. In this lesson, you will learn how to create copies, inspect their behavior, and choose the appropriate approach for your data.
1. What Is a Shallow Copy?
A shallow copy creates a new outer object but does not recursively copy every object contained inside it. The copied container can still refer to the same nested objects as the original.
For a regular NumPy array containing numeric values, array.copy()
creates independent element data. However, for an array with
dtype=object, the array's references are copied while the
referenced Python objects can remain shared.
import numpy as np
original = np.array([10, 20, 30, 40])
shallow_copy = original.copy()
shallow_copy[0] = 999
print("Original:", original)
print("Copy:", shallow_copy)
Output
Original: [10 20 30 40] Copy: [999 20 30 40]
For this numeric array, the copy has its own element storage. Changing an element in the copied array does not affect the original.
ndarray.copy() copies the array's element data, but object
arrays need special care.
2. What Is a Deep Copy?
A deep copy recursively copies nested objects where supported, producing
an independent object structure. Python provides the
copy.deepcopy() function for this purpose.
Deep copying is particularly useful when working with nested Python lists, dictionaries, or NumPy object arrays that contain mutable objects.
import copy
original = [
[10, 20],
[30, 40]
]
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 999
print("Original:", original)
print("Deep copy:", deep_copy)
Output
Original: [[10, 20], [30, 40]] Deep copy: [[999, 20], [30, 40]]
The nested list inside deep_copy is independent from the
corresponding nested list in original.
copy.deepcopy() when you need an
independent nested Python object structure and shallow copying is not
sufficient.
3. Shallow Copy vs Deep Copy
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Outer object | Creates a new outer object. | Creates a new outer object. |
| Nested objects | May continue to be shared. | Recursively copies nested objects where supported. |
| Memory usage | Usually lower for nested structures. | Can require more memory. |
| Processing time | Usually faster for nested structures. | Can take longer for complex structures. |
| Python function | copy.copy() |
copy.deepcopy() |
| Numeric NumPy array | arr.copy() copies its element data. |
Usually unnecessary for a plain numeric array. |
4. Understanding Shallow Copies with Nested Lists
A Python list can contain other lists. A shallow copy duplicates the outer list but keeps references to the same inner lists.
import copy
original = [
[1, 2],
[3, 4]
]
shallow_copy = copy.copy(original)
shallow_copy[0][0] = 100
print("Original:", original)
print("Shallow copy:", shallow_copy)
Output
Original: [[100, 2], [3, 4]] Shallow copy: [[100, 2], [3, 4]]
Both lists show the updated nested value because the inner list is shared.
Changing Only the Outer List
original = [[1, 2], [3, 4]]
shallow_copy = copy.copy(original)
shallow_copy.append([5, 6])
print("Original:", original)
print("Shallow copy:", shallow_copy)
Output
Original: [[1, 2], [3, 4]] Shallow copy: [[1, 2], [3, 4], [5, 6]]
Appending a new element changes only the outer copied list. It does not append the new nested list to the original outer list.
5. Deep Copy with Nested Lists
A deep copy recursively copies the nested list structure. Modifying a nested list in the copied structure does not modify the corresponding nested list in the original.
import copy
original = [
[10, 20],
[30, 40]
]
deep_copy = copy.deepcopy(original)
deep_copy[1][0] = 777
print("Original:", original)
print("Deep copy:", deep_copy)
Output
Original: [[10, 20], [30, 40]] Deep copy: [[10, 20], [777, 40]]
Only the copied nested list changes. The original retains its values.
6. Shallow Copy and Deep Copy with NumPy Object Arrays
NumPy arrays with dtype=object store references to Python
objects. The array's element references and the objects they point to
should be considered separately.
Using ndarray.copy()
import numpy as np
original = np.empty(2, dtype=object)
original[0] = [10, 20]
original[1] = [30, 40]
copied = original.copy()
copied[0][0] = 999
print("Original first item:", original[0])
print("Copied first item:", copied[0])
Output
Original first item: [999, 20] Copied first item: [999, 20]
The array objects are separate, but their elements refer to the same nested lists. Modifying a nested list therefore affects what is seen through both arrays.
Using copy.deepcopy()
import copy
import numpy as np
original = np.empty(2, dtype=object)
original[0] = [10, 20]
original[1] = [30, 40]
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 999
print("Original first item:", original[0])
print("Deep copy first item:", deep_copy[0])
Output
Original first item: [10, 20] Deep copy first item: [999, 20]
In this example, copy.deepcopy() recursively copies the
nested lists referenced by the object array.
7. Choosing the Right Copying Method
| Requirement | Recommended Approach |
|---|---|
| Independent numeric NumPy array | arr.copy() |
| Share array data intentionally | arr.view(), where appropriate |
| Copy a nested Python list without recursively copying its contents | copy.copy() |
| Independently copy nested mutable Python objects | copy.deepcopy() |
| Check whether NumPy arrays share element memory | np.shares_memory(a, b) |
arr.copy() is generally the straightforward way to create
independent element data. Reserve deep copying for nested Python objects
when recursive independence is required.
8. Practical Example: Student Records
Imagine that CIIT TRANING INSTITUTE maintains student marks and wants to create a separate practice dataset for Samadhan, Pradnya, and Samiksha.
import copy
student_records = {
"Samadhan": {"marks": [78, 82]},
"Pradnya": {"marks": [85, 90]},
"Samiksha": {"marks": [92, 95]}
}
# Create a fully independent nested structure
practice_records = copy.deepcopy(student_records)
practice_records["Samadhan"]["marks"][0] = 100
print("Original Samadhan marks:",
student_records["Samadhan"]["marks"])
print("Practice Samadhan marks:",
practice_records["Samadhan"]["marks"])
Output
Original Samadhan marks: [78, 82] Practice Samadhan marks: [100, 82]
The original student records remain unchanged because the nested dictionary and list structures were deep-copied.
9. Common Mistakes
Mistake 1: Assuming a Shallow Copy Duplicates Nested Objects
A shallow copy creates a new outer container, but nested mutable
objects can remain shared. Use copy.deepcopy() when
recursive independence is required.
Mistake 2: Using Deep Copy for Every Numeric Array
A regular numeric NumPy array usually needs only
arr.copy() to obtain independent element data.
Deep copying can add unnecessary overhead.
Mistake 3: Confusing a View with a Copy
A view can share the original array's element data. Use
np.shares_memory() when you need to check whether
arrays share memory.
10. Practice Exercises
Exercise 1: Shallow Copy
Create a nested list, make a shallow copy using
copy.copy(), and change an element inside one of its
nested lists. Observe the original list.
Exercise 2: Deep Copy
Create a nested list and use copy.deepcopy(). Modify
a nested element in the copy and confirm that the original stays
unchanged.
Exercise 3: NumPy Numeric Array
Create a NumPy array of marks, make a copy using
arr.copy(), and update the copied array.
Exercise 4: Object Array
Create a NumPy object array containing nested lists. Use
arr.copy() and inspect what happens when a nested
list is modified.
Exercise 5: Student Records
Create nested records for Samadhan, Pradnya, and Samiksha. Make a deep copy and modify one student's practice marks without changing the original records.
Summary
A shallow copy creates a new outer object but may continue sharing nested objects. A deep copy recursively copies nested objects where supported, making it useful for independent nested data structures.
For ordinary numeric NumPy arrays, array.copy() creates
independent element data. For object arrays containing mutable Python
objects, nested references may still be shared, so
copy.deepcopy() may be appropriate.
Choose the copying method according to your data structure and
requirements. Use np.shares_memory() to investigate
whether NumPy arrays share element memory, and test nested objects
carefully when independent changes are important.