Python Deepcopy vs Shallow Copy: Fix Shared Nested Lists for Good
You copied a list, changed the copy, and the original changed too. Learn the difference between assignment, shallow copy and Python deepcopy, why nested lists and dicts get shared, the grid trap, cycles, custom __deepcopy__ and what deepcopy costs, with tested output.
b = acopies nothing. Both names point to the same object.- A shallow copy (
copy.copy,.copy(),[:]) makes a new outer container but shares the objects inside it. - Python deepcopy (
copy.deepcopy) copies the whole tree, so nested lists and dicts are independent. - deepcopy is slower. In our test it took 13.1 ms for a 2,000 row structure, against 0.0007 ms for a shallow copy.
This bug catches almost every Python developer at least once. You build a default configuration, copy it for a test or a new user, tweak the copy, and later discover the default has changed as well. Nothing is wrong with Python. The copy simply was not as deep as you thought. Understanding Python deepcopy versus a shallow copy fixes this class of bug for good.
Every result below comes from Python 3.13.6 runs on our machine. We use the identity operator is throughout, because it answers the real question: “are these the same object?”
First: names point to objects
In Python, a variable is a name attached to an object. Assignment never copies data. It attaches another name to the same object. Lists, dicts and sets are mutable, so a change made through one name is visible through every other name for that object. That is the root of all copy confusion.
Three options: alias, shallow copy, deep copy
| Operation | New outer object? | New inner objects? |
|---|---|---|
b = a |
No | No |
copy.copy(a) or a.copy() |
Yes | No, shared |
copy.deepcopy(a) |
Yes | Yes, recursively |
The demo: one config, three copies
import copy
cfg = {"name": "prod", "hosts": ["a", "b"], "limits": {"cpu": 2}}
alias = cfg
shallow = copy.copy(cfg)
deep = copy.deepcopy(cfg)
shallow["hosts"].append("c") # change a nested list via the shallow copy
shallow["name"] = "staging" # replace a top-level value via the shallow copy
deep["limits"]["cpu"] = 8 # change a nested dict via the deep copy
print(cfg)
{'name': 'prod', 'hosts': ['a', 'b', 'c'], 'limits': {'cpu': 2}}
Read that output carefully, because it holds the whole lesson:
'c'leaked into the original. The shallow copy shares thehostslist, so appending through it changed the one list both dicts hold.nameis still'prod'. Assigning a new value to a key replaces the reference in the shallow copy’s own outer dict, which is separate.cpuis still 2. The deep copy has its ownlimitsdict.
The identity checks confirm it:
print(alias is cfg, shallow is cfg, shallow["hosts"] is cfg["hosts"], deep["hosts"] is cfg["hosts"])
# True False True False
So the rule is: a shallow copy is safe for replacing values, but not for mutating nested objects in place. If you will mutate nested data, you need a deep copy.
Ways to make a shallow copy
All of these produce the same kind of shallow copy of a list:
nested = [[1, 2], [3, 4]]
copies = [nested[:], nested.copy(), list(nested), copy.copy(nested)]
print([c[0] is nested[0] for c in copies])
print(copy.deepcopy(nested)[0] is nested[0])
# [True, True, True, True]
# False
Every shallow method shared the first inner list. Only deepcopy produced a new one. The same applies to dicts: dict(d), d.copy(), {**d} and copy.copy(d) are all shallow. The unpacking syntax looks like a fresh object, but nested values are still shared. For flat lists of numbers or strings, shallow is all you need.
The grid trap: [[0] * 3] * 3
A close cousin of the copy bug happens when you build nested lists:
grid = [[0] * 3] * 3
grid[0][0] = 1
print(grid)
# [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
grid = [[0] * 3 for _ in range(3)]
grid[0][0] = 1
print(grid)
# [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
Multiplying the outer list repeats a reference to one inner list three times, so changing one row changes “all” of them. The list comprehension creates a new inner list on each pass. This is the standard way to build a 2D grid, board or matrix without NumPy.
Tuples and immutable objects
Copying immutable objects is mostly pointless, and Python knows it. copy.copy of a tuple returns the same tuple. deepcopy is smarter still: it returns the same tuple only when everything inside is immutable.
t = (1, 2, [3])
print(copy.copy(t) is t, copy.deepcopy(t) is t)
# True False
Because this tuple contains a list, deepcopy built a new tuple holding a new list. Strings, numbers and frozensets are never duplicated at all, which saves memory. A tuple is only “fully frozen” if its contents are, as our list vs tuple vs set guide explains.
Cycles and shared references
A naive recursive copy would loop forever on a structure that contains itself. deepcopy keeps a memo dictionary of objects it has already copied, so it handles cycles and preserves sharing:
a = [1]
a.append(a) # a list that contains itself
b = copy.deepcopy(a)
print(b[1] is b, b is not a)
# True True
shared = [0]
d = {"x": shared, "y": shared}
dd = copy.deepcopy(d)
print(dd["x"] is dd["y"])
# True
The copied list points to itself, not to the original. And when two keys shared one list in the original, they still share one (new) list in the copy. deepcopy reproduces the shape of your object graph, not just its values.
Customising deepcopy for your classes
By default, deepcopy copies every attribute of your objects. Sometimes that is wrong. A database connection, file handle or socket should be shared, not cloned. Define __deepcopy__ to control it:
class Conn:
def __init__(self):
self.cache = {}
self.socket = "<live socket>"
def __deepcopy__(self, memo):
new = Conn.__new__(Conn)
new.cache = copy.deepcopy(self.cache, memo) # independent data
new.socket = self.socket # shared resource
return new
c = Conn(); c.cache["k"] = [1]
c2 = copy.deepcopy(c)
print(c2.cache is c.cache, c2.socket is c.socket)
# False True
Always pass memo along when you deep copy attributes, so cycle handling keeps working. __copy__ customises shallow copies the same way. Our classes and objects guide covers these special methods in context.
What deepcopy cannot copy
Some objects represent live operating system resources and refuse to be copied:
import threading
copy.deepcopy({"lock": threading.Lock()})
# TypeError: cannot pickle '_thread.lock' object
The error mentions pickle because deepcopy falls back on the same protocol that pickling uses. Locks, open files, sockets and many C extension objects behave this way. The fix is a custom __deepcopy__ that shares or recreates the resource, as above, or keeping such resources out of the data you copy.
How slow is deepcopy?
deepcopy walks every object, checks the memo and copies recursively, so it costs real time on big structures. We timed 20 runs on a dict with 2,000 rows, each holding a list and a nested dict:
deepcopy 13.1 ms, json 5.1 ms, copy 0.0007 ms
A shallow copy only copies one outer dict, so it is almost free. Round-tripping through json.dumps and json.loads was about 2.5 times faster than deepcopy here, but only works for JSON types: tuples become lists, sets fail, and datetimes need converting. Use it only for plain data, as our Python JSON guide explains. In hot loops, the best fix is often design: avoid copying by building new objects instead of mutating shared ones.
Dataclasses and dataclasses.replace
dataclasses.replace() is a convenient way to make a modified copy, but it is shallow:
from dataclasses import dataclass, field, replace
@dataclass
class Order:
id: int
items: list = field(default_factory=list)
o = Order(1, ["pen"])
r = replace(o, id=2)
r.items.append("ink")
print(o)
# Order(id=1, items=['pen', 'ink'])
The new order shares the items list with the original. Use copy.deepcopy(o) when the fields hold mutable data you will change, or use tuples and frozen=True dataclasses so nothing can be mutated in place. Our dataclasses guide covers frozen classes.
Copies in tests
Shared fixtures are a common source of flaky tests. If several tests receive the same dict from a module-level constant and one of them mutates it, later tests see the change and fail depending on run order. Two fixes:
- Return a fresh object from a pytest fixture with function scope, so each test builds its own.
- Deep copy a shared template at the start of the fixture:
return copy.deepcopy(BASE_CONFIG).
Mutable default arguments, such as def f(items=[]), cause the same kind of leak between calls. Use None as the default and create the list inside the function.
Which one should you use?
- Flat data of numbers and strings: shallow copy.
- Nested data you will mutate: deepcopy.
- Nested data you will only read or replace at the top level: shallow copy is fine.
- Large or hot-path data: avoid copying, or use immutable structures.
- Objects holding resources: define
__deepcopy__.
Debugging tip: when an object changes unexpectedly, print id(x) or test a is b for the suspect parts. Matching identities tell you immediately which objects are shared.
Final thoughts
Most copy bugs come from one misunderstanding: assuming a copy is deep when it is shallow. Remember that assignment copies nothing, that .copy(), slicing and copy.copy share nested objects, and that Python deepcopy copies the whole tree while respecting cycles and shared references. Reach for deepcopy when you will mutate nested data, accept its cost, and customise it for objects that hold resources.
FAQ
What is the difference between copy and deepcopy in Python?
copy makes a new outer object but shares the objects inside it. deepcopy also copies every nested object, so changes to the copy never affect the original.
Why did changing my copied list change the original?
Either you assigned the list instead of copying it, or you made a shallow copy and changed a nested list that both copies share. Use copy.deepcopy for nested data.
Is list slicing a deep copy?
No. Slicing with an empty range creates a shallow copy. The new list holds references to the same inner objects as the original.
Does deepcopy handle circular references?
Yes. deepcopy keeps a memo of objects already copied, so self references and shared objects are reproduced correctly without infinite recursion.
Why does deepcopy raise cannot pickle?
Some objects such as locks, files and sockets cannot be copied. Define a custom deepcopy method that shares or recreates them, or keep them out of copied data.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.