What is the difference between shallow copy, deepcopy and normal assignment operation?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python, assignment (b = a) binds a new name to the same object — both variables point to identical data in memory. A shallow copy (copy.copy(a)) creates a new outer object but shares references to the nested objects inside. A deep copy (copy.deepcopy(a)) creates a fully independent clone — the new object and all its nested objects are separate from the original. Understanding these differences prevents bugs where modifying one variable unexpectedly changes another.
Normal Assignment
Assignment does not copy anything. a and b are two names for the same list object.
Shallow Copy
A shallow copy creates a new container but fills it with references to the same child objects. Changes to nested (mutable) objects are visible through both copies.
Deep Copy
Deep copy recursively copies every object in the hierarchy. Modifying b at any depth does not affect a.
Alternative Shallow Copy Methods
All of these create shallow copies — the outer container is new but nested mutable objects are shared.
Visual Comparison
When to Use Each
| Scenario | Use |
| Both variables should always reflect the same data | Assignment (b = a) |
| Need a new container but nested data can be shared (flat structure) | Shallow copy |
| Need a fully independent clone (nested mutable structures) | Deep copy |
| Immutable data (strings, ints, tuples of immutables) | Assignment (copying has no benefit) |
Custom Copy Behavior
Common Pitfalls
- Assuming assignment creates a copy:
b = amakesbpoint to the same object asa. Any mutation throughbis visible througha. Usecopy.copy()orcopy.deepcopy()when you need an independent object. - Using shallow copy for nested structures:
copy.copy()only copies the top-level container. If the structure contains lists, dicts, or other mutable objects, those are shared. Usecopy.deepcopy()when nested mutable objects must be independent. - Deep copying objects with circular references:
copy.deepcopy()handles circular references using a memo dictionary, but custom__deepcopy__methods must passmemoto recursive calls. Forgettingmemocan cause infinite recursion. - Deep copying large objects unnecessarily:
deepcopyis significantly slower than shallow copy because it recursively clones every nested object. If the structure is flat (no nested mutables), shallow copy is sufficient and much faster. - Expecting
list.copy()to deep copy:list.copy(), slicing ([:]), anddict.copy()all produce shallow copies. They do not recursively copy nested objects. Onlycopy.deepcopy()creates a fully independent clone.
Summary
- Assignment (
b = a) creates a new name for the same object — no copying occurs - Shallow copy (
copy.copy(),list.copy(),[:]) creates a new outer object but shares nested references - Deep copy (
copy.deepcopy()) recursively clones everything — fully independent - Use shallow copy for flat structures (lists of immutables, simple dicts)
- Use deep copy for nested mutable structures (lists of lists, dicts of dicts)
- Implement
__copy__and__deepcopy__on custom classes to control copy behavior
Related reading
- What is the difference between sortedlist vs list.sort?
- What is the difference between tensors and sparse tensors?
- What is the difference between the HashMap and Map objects in Java?
- What is the difference between Travelling Salesman and finding Shortest Path?
- What is the difference between text and new Stringtext?
- What is the difference between using IDisposable vs a destructor in C?
- What is the difference between staticmethod and classmethod in Python?
- What is the difference between subprocess.popen and subprocess.run

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.