Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

copy

Python's copy module provides shallow and deep copying for compound objects. Shallow copies share nested objects, while deep copies recursively duplicate them.

Since
3.13 for copy.replace

How it works

The copy module provides generic shallow and deep copying for compound objects. Shallow copying creates a new outer object but keeps references to contained objects; deep copying recursively copies those contents and tracks already-copied objects with a memo dictionary.

Syntax

copy.copy(obj)
copy.deepcopy(obj[, memo])
copy.replace(obj, /, **changes)

Parameters

NameTypeDescription
objobjectObject to copy.
memodictMemoization dictionary used by deepcopy to track objects already copied during the current copying pass.
changesmappingField values used by copy.replace to construct the replacement object.

Returns

copy.copy returns a shallow copy; copy.deepcopy returns a deep copy; copy.replace returns a new object of the same type with specified fields replaced.

Key points

  • Assignment gives another name to an object; it does not create an independent copy.
  • Use copy.copy for a shallow copy or copy.deepcopy when nested objects must also be duplicated.
  • Shallow copying separates the outer container but retains references to its contents.
  • Deep copying recursively duplicates the contents of compound objects.
  • copy.replace creates a same-type object with selected fields replaced; it was added in Python 3.13.
  • For supported built-in collections, methods such as list.copy(), dict.copy(), and set.copy() are alternatives.

Common mistakes

  • Some runtime and resource-related objects cannot be copied by this module.
  • Functions and classes are returned unchanged rather than duplicated.

Example: Shallow versus deep copy

from copy import copy, deepcopy

original = {"tags": ["python"]}
shallow = copy(original)
deep = deepcopy(original)

original["tags"].append("backend")

print(shallow["tags"])  # ['python', 'backend']
print(deep["tags"])     # ['python']

A shallow copy reuses the nested list, while a deep copy recursively duplicates it.

When to use it

Shallow container duplication

Create an independent outer list or dictionary while intentionally sharing nested values.

Deep configuration cloning

Build an isolated mutable data structure before modifying nested lists, dictionaries, or class instances.

Field-based replacement

Create a modified immutable-style object by replacing selected supported fields.

Interview questions

QWhat is the difference between shallow and deep copying?

copy.copy creates a new compound object and places references to the original's contained objects into it. Mutating a nested mutable value can therefore affect both the original and the shallow copy.

Follow-up: How would you isolate nested mutable values? Use copy.deepcopy, which recursively copies them while tracking objects already processed.

QWhat is the purpose of deepcopy's memo argument?

deepcopy accepts an optional memo dictionary internally used to remember objects already copied during the current operation. This supports recursive structures and avoids repeatedly copying the same object.

Follow-up: Why does this matter for cycles? A previously seen object can be reused from the memo instead of causing unbounded recursion.

QHow can a user-defined class control its copy behavior?

A class can customize copying by defining __copy__ for shallow copies and __deepcopy__ for deep copies. The latter receives the memo dictionary as its argument.

Follow-up: What should a custom __deepcopy__ implementation do with memo? It should use it when recursively copying components so shared references and cycles are preserved.

  • copy.copy
  • copy.deepcopy
  • copy.replace
  • copy.Error
  • __copy__()
  • __deepcopy__()
  • pickle

Source: copy — Shallow and deep copy operations. Checked 2026-10-16.