---
title: "Explain the difference between a shallow copy and a deep copy in Python."  
description: "Explain the difference between a shallow copy and a deep copy in Python."  
author: "Utpal Vishwas"  
published: 2023-06-26  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/158865/explain-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python  
category: "python"  
tags: ["python", "programs"]  
reading_time: 3 minutes  

---

# Explain the difference between a shallow copy and a deep copy in Python.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) between a [shallow copy](https://www.mindstick.com/interview/33945/discuss-the-differences-between-deep-copy-and-shallow-copy-in-java-objects) and a [deep](https://yourviews.mindstick.com/view/87451/a-journey-into-the-deep-dubai-s-underwater-wonderland) copy in Python.

## Replies

### Reply by Aryan Kumar

Sure. In Python, there are two types of copies: shallow [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) and deep copy.

- **Shallow copy:** A shallow copy creates a new object that contains the same references as the original object. This means that if you change the value of an element in the shallow copy, the value of the element in the original object will also change.
- **Deep copy:** A deep copy creates a new object that contains the same data as the original object. This means that if you change the value of an element in the deep copy, the value of the element in the original object will not change.

Here is an example of how to create a shallow copy and a deep copy in Python:

Python

```plaintext
list1 = [1, 2, 3]
list2 = list1[:]  # shallow copy
list3 = copy.deepcopy(list1)  # deep copy

list1[0] = 10

print(list1)
print(list2)
print(list3)
```

This code will print the following output:

Code snippet

```plaintext
[10, 2, 3]
[1, 2, 3]
[1, 2, 3]
```

As you can see, the shallow copy `list2` contains the same references as the original list `list1`. When we change the value of the first element in `list1`, the value of the first element in `list2` also changes.

The deep copy `list3` contains the same data as the original list `list1`, but it does not contain the same references. When we change the value of the first element in `list1`, the value of the first element in `list3` does not change.

Here is a table that summarizes the differences between shallow copy and deep copy:

| Feature | Shallow copy | Deep copy |
| --- | --- | --- |
| References | The shallow copy contains the same references as the original object. | The deep copy contains the same data as the original object, but it does not contain the same references. |
| Changes | If you change the value of an element in the shallow copy, the value of the element in the original object will also change. | If you change the value of an element in the deep copy, the value of the element in the original object will not change. |
| Speed | Shallow copy is faster than deep copy. | Deep copy is slower than shallow copy. |


---

Original Source: https://www.mindstick.com/forum/158865/explain-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
