Deep vs Shallow Copies in Python

python_tutorials

Introduction

In this tutorial, we are going to discuss shallow copies vs deep copies with the help of examples in Python. We will cover the definition of a deep and shallow copy, along with its implementation in the Python language to evaluate the core differences between the two types of copies.

In many of the programs that we write, no matter how basic they are, we end up needing to copy a list or an object for one of many reasons, like computational efficiency. There are two ways to do that, either make a deep copy or a shallow copy. Before we discuss the differences between the two, let’s first understand what deep and shallow copies exactly are.

Deep Copies in Python

A deep copy makes a new and separate copy of an entire object or list with its own unique memory address. What this means is that any changes you make in the new copy of the object/list won’t reflect in the original one. This process happens by first creating a new list or object, followed by recursively copying the elements from the original one to the new one.

To put it briefly, both of the

To finish reading, please visit source site