Skip to main content
Intermediatecomparison Verified ~6 min

What is the difference between shallow copy and deep copy in Python (copy.copy vs copy.deepcopy)?

Source: Python tutorial: data structures

Quick answer

Assignment only adds a new name for the same object. A shallow copy (copy.copy, list(x), x[:], dict.copy()) creates a new outer container but shares the nested objects, so changing a nested list through the copy changes the original. copy.deepcopy copies nested objects recursively, so the copies are independent.

In Python, b = a does not copy anything: both names point to the same object. To copy, use the copy module (documented at docs.python.org/3/library/copy.html).

Example with a nested list

main.py
import copy

a = [[1, 2], [3]]
b = copy.copy(a)        # shallow: new outer list, same inner lists
c = copy.deepcopy(a)    # deep: new outer list AND new inner lists

a[0].append(99)         # mutate a nested object
a.append([4])           # change the outer list

print(a)   # [[1, 2, 99], [3], [4]]
print(b)   # [[1, 2, 99], [3]]      <- sees the nested change, not the new item
print(c)   # [[1, 2], [3]]          <- fully independent
print(b[0] is a[0], c[0] is a[0])    # True False

(Output from Python 3.12.)

What each one does

OperationOuter containerNested objects
b = asame objectsame objects
shallow copy: copy.copy(a), list(a), a[:], a.copy(), dict(d)newshared
copy.deepcopy(a)newcopied recursively

How deepcopy works

deepcopy walks the object graph and keeps a memo dictionary of objects already copied, so shared references stay shared inside the copy and cycles do not loop forever (for an object that points to itself, the copy points to itself, not to the original). Classes can customise it with __copy__ and __deepcopy__.

Details

  • Immutable objects such as ints, strings and tuples are not really copied: copy.copy(t) returns the same tuple object t (checked on 3.12). copy.deepcopy(t) of a tuple of immutables also returns the same object. A tuple that contains a list holds a shared list after copy.copy.
  • Cost: deepcopy is slower and uses more memory. Do not use it on huge structures in hot loops; copy only the part you change, or use immutable data.
  • Not copyable things: open files, sockets and locks cannot be deep copied.
  • Typical bug: copying a default config dict with dict.copy() and then editing cfg["db"]["host"] also changes the shared default. Use copy.deepcopy(DEFAULTS).
  • Another classic: [[0] * 3] * 3 makes three references to one row, a copy problem in disguise.

Which to choose

Use a shallow copy when the nested values are immutable or you will not change them. Use deepcopy when you need a fully independent structure.

Animated explanation

Assignment

  • New name, same object

Shallow copy

  • New outer container
  • Nested objects shared

Deep copy

  • copy.deepcopy
  • Nested objects copied, independent
Editing a nested setting through a shallow copy changes the original too.

Key points

  • Assignment copies the reference, not the object.
  • A shallow copy makes a new outer container but shares the nested objects.
  • deepcopy copies recursively, uses a memo for shared references and cycles, and gives independent data.
  • Immutable objects are usually returned as is; mutable nested objects are the risk.
  • Deepcopy is costly: use it only where independence is needed.

Common mistakes

  • Thinking b = a makes a copy.
  • Using dict.copy() or list(x) on nested data and then editing nested values.
  • Using deepcopy everywhere and paying the performance cost.
  • Writing [[0]*n]*m for a 2D grid.
  • Trying to deepcopy objects that hold files or locks.

Prerequisites

  • Mutable vs immutable objects
  • References and identity (is vs ==)

Best practices

  • Deepcopy default configuration templates before modifying them.
  • Prefer immutable data (tuples, frozen dataclasses) so copying is unnecessary.
  • Define __deepcopy__ for classes that hold resources.
  • Test copy behaviour with is checks on nested objects.

Likely follow-ups

  • What does the memo argument of copy.deepcopy do?
  • How do you create a 2D list in Python without sharing rows?
  • What are __copy__ and __deepcopy__ used for?
  • Is slicing a list (a[:]) a shallow or deep copy?
  • What is the difference between a copy and a view in NumPy?

References

Discussion