You’ll never forget Shallow Copy and Deep Copy in Python after reading this…

Amsavalli Mylasalam
Variablz Academy
Published in
4 min readJul 14, 2022
Deep Copy Vs Shallow Copy — Image Credits: aatomz research

Indeed, one of the most common interview questions I’ve been asked in the Python interviews is the difference between Shallow copy and Deep Copy.

Copying the compound objects, such as those containing lists, class instances or nested lists in Python/NumPy, is too dangerous.

To understand why it is too dangerous, we should know the mechanism behind various copy operations. So In this article, I am going to explain it.

Various methods to copy an object in Python

  1. Using Reference
  2. Using Shallow copy
  3. Using Deep Copy

1. Using Reference (Both Pain and Benefits)

We can use the assignment operator (=) to get a copy of the existing object. It won’t create a new object, but it will create a reference to the existing object. It means that whatever the changes we are doing with the original object will affect the reference object or vice versa.

For example, If you beat any cat, both the original and reference cat feels the pain. If you give food to any cat, both original and reference cat will have food. i.e., both pain and benefits for both cats

Reference: Image Credits to aatomz research

Input:

import pandas as pd
import numpy as np
original_array=np.array([5,6,7,8,9])
ref_array=original_array
print(f'Original: {original_array}')
print(f'Reference: {ref_array}')

Output:

Original: [5 6 7 8 9]
Reference: [5 6 7 8 9]

Now we are changing the particular element in the original array using the assignment operator and notice that the value has changed in both the original and reference object.

original_array[2]=4print(f’Original:{original_array}’)
print(f’ref_array:{ref_array}’)

Output:

Original:[5 6 4 8 9]
ref_array:[5 6 4 8 9]

2. Using Shallow Copy (Only Pain, No Benefits)

We can add new items into the shallow copied object, but we cannot modify the existing items.

For example, We can give food to the copied cat, but it doesn’t mean we are giving food to the original cat. However, if we beat mocked cat with the hammer, the original cat also feels the pain 🤣🤣🤣 i.e., only pain, no benefits.

Shallow Copy — Image Credits to aatomz research

The copy method copies the reference of the compound objects, i.e., it is the same as the reference method but only for nested items. Therefore, this method is called shallow copy or partial copy.

So, appending a new item to the copied compound objects won’t affect the original object, but modifying the copied nested items will affect the original object.

Input:

original=np.array([1,3,5,7])new=copy.copy(original)print(f'Original :{original}')
print(f'New Value :{new}')

Output:

This means it will create new and independent object with same content.
To verify this, we print the original and new.

Original :[1 3 5 7]
New Value :[1 3 5 7]

In this below example, We are appending an item to the original object, but this change will not affect the copied item and vice versa.

original=np.append(original,[9])print(f'After appending 9 the Orginal if: {original}')
print(f'New Value is : {new}')

Output:

After appending 9 the Orginal is: [1 3 5 7 9]
New Value is : [1 3 5 7]

Now we modify the existing element in the original object and notice that it will also reflect in the copied item and vice versa.

Input:

import copyold = [[1,3,5],[2,4,6]]
new = copy.copy(old)
old[1][1] = 8print(“Old list:”, old)
print(“New list:”, new)

Output:

Old list: [[1, 3, 5], [2, 8, 6]]
New list: [[1, 3, 5], [2, 8, 6]

3. Using Deep Copy (No Pain, No Benefits)

Python/NumPy has a deep copy function to avoid this pitfall while copying the object.

After the deep copy, whatever the modification changed in copied object won’t affect the original object or vice versa, i.e., no pain and no benefits for the original/copied cat 🤷🏻‍♀️

deep copy: Image credits to aatomz research

The deep copy function copies each item recursively from the original array into the copied array. Therefore the nested objects won’t be affected because of the modification done to the original object or vice versa.

Let’s check the below example. First, we modify the element from the original object and notice that the deep copied item won’t get affected.

Input:

import copyold = [[1,3,5],[2,4,6]]
new = copy.deepcopy(old)
old[1][2] = 8print(“Old list:”, old)
print(“New list:”, new)

Output:

Old list: [[1, 3, 5], [2, 4, 8]]
New list: [[1, 3, 5], [2, 4, 6]]

I hope you enjoyed the concept of shallow and deep copy methods in Python/NumPy.

Don’t forget to clap if you like it; I also welcome your suggestions.

Follow me on LinkedIn

--

--