deep copy

In Python, deep copy refers to creating a new object that is a complete, independent clone of the original object, including all the objects it refers to, recursively. This means that if you modify the deep-copied object or any nested objects inside it, the original object remains unchanged.

You typically use the copy.deepcopy() function from the copy module to make deep copies.

Deep copying is especially important when you’re working with complex, nested data structures like lists of dictionaries, or objects that contain other mutable objects.

See also: shallow copy

Example

Here’s how you can use deepcopy() to duplicate nested data structures:

Python
>>> import copy

>>> original = {"numbers": [1, 2, 3], "info": {"name": "Alice"}}
>>> clone = copy.deepcopy(original)

>>> clone["numbers"].append(4)
>>> clone["info"]["name"] = "Bob"

>>> original
{'numbers': [1, 2, 3], 'info': {'name': 'Alice'}}
>>> clone
{'numbers': [1, 2, 3, 4], 'info': {'name': 'Bob'}}

As you can see, changes to clone don’t affect the original object, even in nested elements.

Tutorial

How to Copy Objects in Python: Shallow vs Deep Copy Explained

Understand the difference between shallow and deep copies in Python. Learn how to duplicate objects safely using the copy module and other techniques.

advanced python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated June 20, 2025