How can I create a copy of an object in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a copy of an object in Python is not one single operation. The right technique depends on whether you want a new reference, a shallow copy, or a deep copy of nested state. The key distinction is simple: shallow copies duplicate the outer container, while deep copies recursively duplicate the contained objects as well.
Assignment Is Not Copying
The assignment operator binds another name to the same object. It does not create a new one.
Both variables refer to the same list, so the mutation is visible through both names.
This is the first thing to check when someone says "my copy changed too." In many cases there was never a copy at all.
Shallow Copy
A shallow copy creates a new outer object but keeps references to the same nested objects inside it.
Appending a new top-level element affects only shallow, because the outer list is different. But modifying shallow[0] affects both, because the nested inner list is shared.
Built-in containers also have common shallow-copy shortcuts:
Those are still shallow copies.
Deep Copy
A deep copy recursively copies nested objects too.
Now the nested lists are independent, so changing one does not affect the other.
Use deep copy when you truly need a separate object graph. Do not use it automatically everywhere, because it can be expensive and may copy much more than you intend.
Choosing Between Shallow and Deep Copy
A shallow copy is usually enough when:
- the object contains only immutable values
- you want a new container but shared members are acceptable
- you know nested state will not be mutated
A deep copy is more appropriate when:
- nested mutable objects must be independent
- you are cloning configuration or state for isolated modification
- the object graph is small enough that recursive copying is acceptable
The correct choice is about ownership of nested state, not just about syntax.
Copying Custom Objects
For user-defined classes, copy.copy() and copy.deepcopy() work on instance attributes, but sometimes the default behavior is not what you want. In that case, implement __copy__ and __deepcopy__.
This gives you explicit control over which parts should be shared and which parts should be duplicated.
Dataclasses and Immutable Design
Sometimes the best answer is to avoid mutable shared state instead of copying it heavily. For dataclasses, dataclasses.replace can be clearer when you want a modified copy of a mostly immutable object.
That is often a better design than performing deep copies of complex mutable objects.
Common Pitfalls
The most common mistake is confusing assignment with copying. A second variable name is not a duplicate object.
Another issue is using a shallow copy when nested mutable objects need to be independent. The outer container changes correctly, but inner objects still leak mutations.
Developers also sometimes reach for deepcopy too early. Deep copying large object graphs can be slow and may duplicate resources or state that should not be cloned blindly.
Finally, if a class manages external resources such as files, sockets, or locks, copying semantics may need careful design rather than a default generic copy.
Summary
- Assignment creates another reference, not a copy.
- Use shallow copy when you need a new outer object but can share nested members.
- Use deep copy when nested mutable state must be independent.
- Implement
__copy__and__deepcopy__for custom copy behavior when needed. - Prefer clearer immutable designs when copying complex mutable state becomes awkward.
Related reading
- How can I create a list of elements from an iterator convert the iterator to a list?
- How can I delete a file or folder in Python?
- How can I determine a Python variable's type?
- How can I disable logging while running unit tests in Python Django?
- How can I display an image from a file in Jupyter Notebook?
- How can I display full non-truncated dataframe information in HTML when converting from Pandas dataframe to HTML?
- How can I do a line break (line continuation) in Python (split up a long line of source code)?
- How can I do a line break line continuation in Python split up a long line of source code?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.