Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Practical Pages

Shallow and deep copies in Python

Concept page on copying in Python: assignment versus shallow and deep copies, the copy module, list and dict copy methods, nested data and custom copy hooks.

Assignment never copies an object; it only adds another name for it. When you need an independent object you must copy it explicitly, and Python gives you two levels. A shallow copy makes a new outer container but reuses the same inner objects. A deep copy recursively copies everything, so the result shares nothing mutable with the original.

For flat data (a list of numbers, a dict of strings) a shallow copy is enough. For nested data (lists of lists, dicts holding lists, objects holding collections) a shallow copy leaves the inner structures shared, which is the source of many surprising bugs.

Python
import copy

shallow = copy.copy(data)
deep = copy.deepcopy(data)

Try it

Python Assignment, shallow copy, deep copy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python The everyday copy idioms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Copying objects, and shared references inside

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Which copy do you need?

You have Use
Flat list, dict or set (only immutable items) x.copy(), list(x), dict(x), x[:]
Nested mutable containers copy.deepcopy(x)
Immutable values (int, str, tuple of immutables) no copy needed
A dataclass instance dataclasses.replace(obj, ...) for a changed shallow copy, or copy.deepcopy

Usage notes

  • copy.copy and copy.deepcopy work on most objects, but not on things tied to the outside world such as open files, sockets and locks.
  • deepcopy remembers what it has already copied (a memo dictionary), so shared references stay shared and cyclic structures do not loop forever.
  • Deep copies can be slow and memory hungry for large structures. Often a better design is to avoid mutating shared data and build new lists with comprehensions.
  • Immutable objects are returned as-is by copy.copy (and by deepcopy, unless a tuple contains mutable items). copy.copy("abc") is "abc" holds for the same object.
  • Customize copying in your class with __copy__(self) and __deepcopy__(self, memo). Without them, Python uses the pickle protocol hooks (__reduce_ex__).
  • list * n and [x] * n copy references, not objects; use a comprehension for rows of independent lists (see mutable and immutable types).
  • dict.copy(), {**d} and d | {} are all shallow.
  • Slicing a list always makes a shallow copy. Slicing a NumPy array, in contrast, returns a view onto the same memory.