Python Shallow vs Deep Copy

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
5 Min Read
Shallow Copy vs. Deep Copy in Python

Understand the differences between Shallow Copy and Deep Copy (Shallow Copy vs. Deep Copy) in Python. These are core programming concepts and you should know how copy works behind the scenes.

Note: The syntax used in the below section is for Python 3. You may change it to use a different version of Python.

Shallow Copy vs. Deep Copy

Must Read – 9 Ways to Copy a File in Python

The difference between shallow and deep copy

A shallow copy operation creates a new object that stores the reference of elements of the original object. So, any changes in the new object also affect the original and vice versa.

While, in deep copy, the new object stores the copy of all elements of the original object. Thus, the result is a fresh list separate from the original one. Hence, it can be changed without affecting the original object.

In summary, when you change the deep copy of a list, the old list remains as is. But shallow copy causes changes in both the new and the old list.

The diagram below represents the difference between the shallow and the deep copy.

Shallow Copy vs. Deep Copy
Shallow vs. Deep copy

How to perform a shallow and deep copy?

When creating a shallow copy, use the assignment operator(=) to create them.

With the Copy module, you can create a shallow copy using the below syntax:

import copy
copy.copy(object_name)

For a deep copy, the following code can be used:

import copy
copy.deepcopy(object_name)

In the next section, a few programs are implemented to demonstrate the Copy Module in Python 3.

Understand by examples

Here is a simple program to demonstrate the difference between Shallow and Deep copy.

import copy

a = [ [1, 2, 3], [4, 5, 6] ]
b = copy.copy(a)

c = [ [7, 8, 9], [10, 11, 12] ]
d = copy.deepcopy(c)

print(a)
print(b)

a[1][2] = 23
b[0][0] = 98

print(a)
print(b)

print("\n")

print(c)
print(d)

a[1][2] = 23
b[0][0] = 98

print(c)
print(d)

The output will come as:

Example output

Key differences

Here is a table summarizing the key differences between shallow copy and deep copy in Python:

FeatureShallow CopyDeep Copy
What it doesCreates a new object that references the same objects as the original object.Creates a new object that contains copies of all the objects referenced by the original object.
When to use itWhen you only need to copy the top-level structure of an object.When you need to copy the entire object hierarchy, including nested objects.
SpeedFasterSlower
Memory usageLess memory-intensiveMore memory-intensive
AvailabilityAvailable in the standard libraryRequires the copy module
The difference between shallow and deep copy

The following are some more topics which could be of interest to you. Please check them out.

Python vs C++ – Understand the Differences
Python Sets vs Lists – Check Out the Differences
Generators vs List Comprehensions in Python
Map vs List Comprehension Difference
Difference Between List Append and Extend
Python XRange vs Range Differences

Summary

Here is a simple explanation of each feature:

  • A shallow copy creates a new object that references the same objects as the original one. This means that changes done in the original object will also be reflected in the copy. For example, consider you have a list of numbers. Now, if you do a shallow copy, all changes will also be made to the original list.
  • You should use shallow copy when you only need to copy the top-level structure of an object. This usually happens when you copy simple objects, such as lists or strings.
  • Shallow copy is faster than deep copy because it does not need to create copies of all the objects referenced by the original object.
  • Shallow copy is less memory-intensive than deep copy because it does not need to create copies of all the objects referenced by the original object.
  • A shallow copy is available in the standard library, so you do not need to install additional modules to enable it. Deep copy requires the copy module, which is a standard library module.

    We hope this helps! Let us know if you have any other questions.

    Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.

    Enjoy coding,
    TechBeamers.

    Share This Article
    Subscribe
    Notify of
    guest

    0 Comments
    Newest
    Oldest
    Inline Feedbacks
    View all comments