Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Shallow Copy and Deep Copy


Shallow Copy and Deep Copy

In Python, you cannot copy an object using the equal to (=) operator. If you copy that way, then it only creates a new variable and assigns the original object reference to the new variable.

Example
numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = numbers_list

print("Numbers list")
print(id(numbers_list))
print("Copy numbers list")
print(id(copy_numbers_list))
Numbers list
4508244480
Copy numbers list
4508244480

As you can see, both objects' IDs were the same when we copied an object using the equal to (=) operator.


Shallow Copy in Python

A shallow copy creates a new object that is a copy of the original object.

Shallow copies do not create copies of nested objects, rather they copy their references.

For shallow copy and deep copy, use copy module in Python.

You can create a shallow copy using the copy() method or the copy module's copy() function.

Example
import copy as cp

numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = cp.copy(numbers_list)

print(copy_numbers_list)
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]

In the above example, we used the copy() function from the copy module to perform a shallow copy operation on a nested list.


Add a New Nested Object Using a Shallow Copy

The copied object and the original object are different from each other after the shallow copy operation. Therefore when you append a new nested object to the original object, it won't appear in the copied object.

Example
import copy as cp

numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = cp.copy(numbers_list)

numbers_list.append([4, 4, 4])

print("Numbers list")
print(numbers_list)
print("Copy numbers list")
print(copy_numbers_list)
Numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]
Copy numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]

As you can see from the output, the copied object does not contain the appended nested object.


Change Nested Objects Using a Shallow Copy

When you make changes to the nested object, those changes also reflect in the copied object while using the shallow copy.

Example
import copy as cp

numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = cp.copy(numbers_list)

numbers_list[2][2] = "Red"
# copy_numbers_list[2][2] = "Red"

print("Numbers list")
print(numbers_list)
print("Copy numbers list")
print(copy_numbers_list)
Numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 'Red']]
Copy numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 'Red']]

In the above example, we used shallow copy for the copy operation. In this case, we made some changes to the original object (numbers_list), and those changes also appeared in the copied object (copy_numbers_list).


Example

Deep Copy in Python

A deep copy creates a new object that includes copies of all nested objects from the original object.

Deep copy creates an independent copy of the original object and all its nested objects.

To perform a deep copy operation, use the deepcopy() function from the copy module in Python.

import copy as cp

numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = cp.deepcopy(numbers_list)

print("Numbers list")
print(numbers_list)
print("Copy numbers list")
print(copy_numbers_list)
Numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Copy numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]

In the above example, to perform a deep copy operation on a nested list, we use the deepcopy() function from the copy module.


Change Nested Objects Using a Deep Copy

While using the deep copy, changes to the nested object do not reflect in the copied object.

import copy as cp

numbers_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

copy_numbers_list = cp.deepcopy(numbers_list)

numbers_list[2][2] = "Red"

print("Numbers list")
print(numbers_list)
print("Copy numbers list")
print(copy_numbers_list)
Numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 'Red']]
Copy numbers list
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]

In the above example, we used deep copy for the copy operation. In this case, we made some changes to the original object (numbers_list), and those changes did not appear in the copied object (copy_numbers_list).