---
source: https://iinventedlight.com/library/notes/python-copy.md
title: "Python copy: shallow copy, deepcopy and copy.replace"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, copy - Shallow and deep copy operations; python/cpython (GitHub), Lib/copy.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: copy"
tags: [python, copy, deepcopy, mutability, stdlib]
client: Librarian
sources:
  - id: S1
    title: "copy - Shallow and deep copy operations"
    publisher: Python docs
    url: https://docs.python.org/3/library/copy.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/copy.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/copy.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python copy: shallow copy, deepcopy and copy.replace
**Answer:** Use `copy.copy(obj)` for a shallow copy and `copy.deepcopy(obj)` when nested mutable objects must not be shared. Assignment does not copy objects; it only creates bindings between a target and an object.

## Examples

```python
import copy
from dataclasses import dataclass

grid = [[0, 0], [0, 0]]
alias = grid                    # same object
shallow = copy.copy(grid)       # new outer list, same inner lists
deep = copy.deepcopy(grid)      # new inner lists too

shallow[0][0] = 1
print(grid[0][0])               # 1 - inner list is shared
print(deep[0][0])               # 0

@dataclass(frozen=True)
class Point:
    x: int
    y: int

p2 = copy.replace(Point(1, 2), y=5)   # Python 3.13+
```

## Shallow versus deep

- The difference only matters for compound objects, such as lists or class instances.
- A shallow copy constructs a new compound object and inserts references to the objects found in the original.
- A deep copy also builds a new compound object, but recursively fills it with copies of the originals.
- `list.copy()`, `dict.copy()`, `set.copy()` and `sequence[:]` also make shallow copies, but they can return the base type for a subclass, while `copy.copy()` normally keeps the same type.

## Deep copy pitfalls

Recursive objects may cause a recursive loop, and deep copy may copy too much, such as data meant to be shared. `deepcopy()` avoids these problems with a memo dictionary of objects already copied, and lets classes override the copying.

## Customizing

- Define `__copy__(self)` and `__deepcopy__(self, memo)`. Inside `__deepcopy__`, call `deepcopy(component, memo)` for components and treat the memo as opaque.
- Classes can also use the same pickle interfaces that control pickling.
- `copy.replace(obj, **changes)` (3.13+) creates a new object with fields replaced; it supports named tuples, dataclasses and classes that define `__replace__()`.

## Not copied

Modules, methods, stack frames, files and sockets are not copied. Functions and classes are returned unchanged.

## See also

- [python-dataclasses](https://iinventedlight.com/library/notes/python-dataclasses/)
- [python-collections-containers](https://iinventedlight.com/library/notes/python-collections-containers/)
- [python-dict-methods](https://iinventedlight.com/library/notes/python-dict-methods/)

## Claims

- id: C1
  claim: Assignment does not copy objects.
  source: S1
  evidence: "Assignment statements in Python do not copy objects"

- id: C2
  claim: deepcopy keeps a memo dictionary.
  source: S1
  evidence: "keeping a memo dictionary of objects already copied during the current copying pass"

- id: C3
  claim: copy.copy makes a shallow copy.
  source: S2
  evidence: "Shallow copy operation on arbitrary Python objects."

## Sources

- [S1] copy - Shallow and deep copy operations. Python docs. https://docs.python.org/3/library/copy.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/copy.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/copy.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, copy - Shallow and deep copy operations; python/cpython (GitHub), Lib/copy.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
