Python Shallow Copy: What It Copies and What It Doesn't
Understand how Python's shallow copy works: what it copies, why nested objects remain shared, and when to use it. Includes practical examples and common pitfalls.

When you copy a Python object, you might expect a completely independent duplicate. But a shallow copy creates a new container while keeping references to the same inner objects. This behavior is often misunderstood and leads to subtle bugs when nested data is modified.
What a Shallow Copy Actually Copies
When you create a shallow copy in Python, Python builds a new container object—such as a list or dictionary—and populates it with references to the same elements as the original. The outer structure is independent, but the inner objects are shared. This means that modifying the copy's structure (adding or removing elements) does not affect the original, but modifying a shared nested object does affect both.
Consider a list of lists:
original = [[1, 2], [3, 4]]
shallow = original.copy()
shallow.append([5, 6]) # Only the outer list changes
shallow[0].append(99) # Shared inner list changes both
print(original) # [[1, 2, 99], [3, 4]]
print(shallow) # [[1, 2, 99], [3, 4], [5, 6]]The outer list is a new object, but the inner lists are the same objects in memory.
Creating a Shallow Copy in Python
Python provides several ways to create a shallow copy. The copy module's copy() function is the most general, but many built-in types have their own methods.
import copy
# Using copy.copy
shallow1 = copy.copy(original)
# Using list.copy() for lists
shallow2 = original.copy()
# Using slicing for lists
shallow3 = original[:]
# Using dict.copy() for dictionaries
original_dict = {"a": [1, 2], "b": [3, 4]}
shallow_dict = original_dict.copy()For custom classes, copy.copy() can use the class's __copy__ method. If that method is not defined, it may rely on the pickle protocol's __reduce_ex__, so objects that cannot be reduced may raise an error.
The Shared Reference Problem
The most common bug with shallow copies is assuming that all nested data is independent. When you mutate an object that appears in both the original and the copy, the change propagates to both. This often surfaces when copying a list of dictionaries or a list of custom objects.
users = [{"name": "Alice", "roles": ["admin"]}, {"name": "Bob", "roles": ["user"]}]
users_copy = users.copy()
users_copy[0]["roles"].append("editor")
print(users[0]["roles"]) # ['admin', 'editor']
print(users_copy[0]["roles"]) # ['admin', 'editor']The dictionary itself is shared because the shallow copy only copies the reference to the dictionary, not its contents.
Shallow Copy vs Deep Copy
The alternative to a shallow copy is a deep copy, which recursively copies all nested objects. The copy.deepcopy() function creates a fully independent duplicate.
| Aspect | Shallow Copy | Deep Copy |
|---|---|---|
| Outer container | New object | New object |
| Nested objects | Shared references | Recursively copied |
| Memory usage | Low (only new outer structure) | Higher (duplicates all objects) |
| Speed | Fast (no recursion) | Slower (recursive traversal) |
| Use case | When nested objects are read-only | When full independence is required |
Choosing between them depends on whether you need to isolate changes to nested data. If the nested data is truly immutable—for example, strings, numbers, or frozensets—a shallow copy behaves like a deep copy in practice because those objects cannot be changed in place.
Performance and Memory Tradeoffs
Shallow copies are cheap because they avoid recursion and object duplication. Creating a shallow copy of a large list is O(n) in the number of elements, but each element is just a reference. Deep copies are O(total size of the object graph) and can be significantly slower and more memory-intensive.
This matters in performance-sensitive code when you only need to protect the outer structure. For example, if you pass a list to a function that may append or remove top-level items but never mutates nested objects, a shallow copy is sufficient and avoids the cost of a deep copy.
Common Pitfalls and How to Avoid Them
One subtle point is that copy.copy() is not guaranteed to return a new object. For immutable container types such as tuples and frozensets, Python may return the same object because the container itself cannot be changed. This is harmless by itself, but an immutable container can still contain mutable objects; a shallow copy does not deep-copy those inner objects.
Another pitfall is using a shallow copy on a custom class without understanding its internal references. If your object contains a mutable attribute, that attribute remains shared. If you need full isolation, either implement __deepcopy__ or use copy.deepcopy().
When you need a shallow copy for a specific type, prefer the built-in methods (list.copy(), dict.copy()) over copy.copy() because they are direct and explicit. For nested data that must be independent, use copy.deepcopy() and be aware of its performance cost.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.