shallow copy

In Python, a shallow copy is a new object that stores references to the original data from an existing object. It’s a common operation in collections, such as lists and dictionaries, where the items in the copy hold references to the items in the original collection rather than copying the items themselves.

In shallow copies, if the original collection contains mutable objects, such as lists or dictionaries, then changes to those objects will be reflected in both the original and the shallow copy. However, changes to the outer container—like adding or removing items—won’t affect the other.

When you make a shallow copy of a collection, you’re essentially creating a new collection with the same references as the original one.

See also: deep copy

Example

Here’s how you can make a shallow copy of a list using the copy module and see how changes to inner objects affect both lists:

Python
>>> import copy

>>> original = [[1, 2], [3, 4]]
>>> shallow = copy.copy(original)

>>> shallow[0][0] = "X"
>>> original
[['X', 2], [3, 4]]
>>> shallow
[['X', 2], [3, 4]]

>>> shallow.append([5, 6])
>>> original
[['X', 2], [3, 4]]
>>> shallow
[['X', 2], [3, 4], [5, 6]]

Notice how modifying an inner element affects both lists, but adding a new element only affects the copy.

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