Deep Copy and Shallow Copy in Python (original) (raw)

Last Updated : 5 Feb, 2026

In Python, assignment statements create references to the same object rather than copying it. Python provides the copy module to create actual copies which offer functions for **shallow (copy.copy()) and **deep (copy. deepcopy ()) copies. In this article, we will explore the main difference between Deep Copy and Shallow Copy.

Deep copy in Python

Syntax of Python Deepcopy

copy.deepcopy()

Example:

Python `

import copy

a = [[1, 2, 3], [4, 5, 6]]

Creating a deep copy of the nested list 'a'

b = copy.deepcopy(a)

Modifying an element in the deep-copied list

b[0][0] = 99 print("b: ", b) print("a: ", a)

`

Output

b: [[99, 2, 3], [4, 5, 6]] a: [[1, 2, 3], [4, 5, 6]]

**Explanation:

Shallow copy in Python

Syntax of Python Shallowcopy

copy.copy()

Example:

Python `

import copy

a = [[1, 2, 3], [4, 5, 6]]

Creating a shallow copy of the nested list 'original'

b = copy.copy(a)

Modifying an element in the shallow-copied list

b[0][0] = 99

Printing the original and shallow-copied lists

print("b: ", b) print("a: ", a)

`

Output

b: [[99, 2, 3], [4, 5, 6]] a: [[99, 2, 3], [4, 5, 6]]

**Explanation: