Menu Close

Python Shallow and Deep Copy

Python shallow and deep copy

In this Python article, you will learn all about Python shallow and deep copy with the help of examples.

Here we are going to use Python’s built-in copy module to create and deep and shallow copy, but before going further in this article, you should have knowledge about Python equal to (=) operator.

Copy an Object in Python


Till now, we were using Python equal to the ( = ) operator to copy an object. Most people think is equal to the operator to create a separate object but they think wrong. It only creates a new variable that shares the reference of the original variable value.

Let’s understand this by using an example so that you don’t have any confusion regarding this.

Example: copy of an object


old_list = [1,2,3,4]

# copy of old_list
new_list = old_list

print("ID of the old_list:- ", id(old_list))
print("ID of the new_list:- ", id(new_list))

Output


ID of the old_list:-  2346362234080
ID of the new_list:-  2346362234080

As you can see above output ID of both lists (old_list and new_list) are the same. If a change in any list, it will reflect in both lists. let’s see it by an example as well.

Example: Adding a new item to the old_list


old_list = [1,2,3,4]

# copy of old_list
new_list = old_list

old_list.append(5)


print("Value of the old_list:- ", old_list)
print("Value of the new_list:- ", new_list)

Output


Value of the old_list:-  [1, 2, 3, 4, 5]
Value of the new_list:-  [1, 2, 3, 4, 5]

But, sometimes you may want to change the only original value that does not reflect in the new value or vice-versa. To do this, Python provides a built-in copy module that is used to create the copies of the object.

There are two ways to create copies of an object in Python.

  • Shallow copy
  • Deep copy

Python copy module

copy module in Python is a built-in module that is mostly used to create a deep and shallow copy in Python. copy module provides two important functions deepcopy() and copy() to create Python shallow and deep copy.

Shallow Copy

A shallow creates a new object that stores a reference of the original value. To create a shallow in Python, we will use the copy module copy() function.

A shallow copy does not create a copy of the nested object, instead, it just copies the reference of the original object.
This means that it does not create copies of the child or nested objects.

Let’s understand this by using an example.

Example: Create a copy using shallow copy.


import copy

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# copy of old_list
new_list = copy.copy(old_list)

print("Value of the old_list:- ", old_list)
print("Value of the new_list:- ", new_list)

When you run the above program, the output will be:


Value of the old_list:-  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Value of the new_list:-  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

As you can see in the above program, we defined a nested list and also created a shallow copy using the copy() function.

copy() function creates a copy of the original object with the same content. To prove this, we printed both of the lists ( old_list and new_list).

Example: Adding a new list to the old_list using shallow copy.


import copy

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# copy of old_list
new_list = copy.copy(old_list)

# add new list to old_list
old_list.append([10,11,12])


print("Value of the old_list:- ", old_list)
print("Value of the new_list:- ", new_list)

When you run the above program, the output will be:


Value of the old_list:-  [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
Value of the new_list:-  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

As you can see in the above Python program, we have created a new shallow copy using the copy() function. Then we added a new list in the old_list. This new sublist was not copied in new_list because shallow copy does create the reference of the nested list.
However, if you make any changes in the nested object it will reflect the original objects.

Example: Adding a new nested object using shallow copy:


import copy

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# copy of old_list
new_list = copy.copy(old_list)

# add new list to old_list
new_list[0][0] = 20


print("Value of the old_list:- ", old_list)
print("Value of the new_list:- ", new_list)

Output


Value of the old_list:-  [[20, 2, 3], [4, 5, 6], [7, 8, 9]]
Value of the new_list:-  [[20, 2, 3], [4, 5, 6], [7, 8, 9]]

In the above program, we made changes in new_list i.e new_list[0][0] = 20.Both sublists old_list and new_list were modified. This is because both lists share the reference of some nested objects.

Deep Copy

A deep copy is used to create the deep copy of the object and recursively add the nested object present in the original objects. It means in deep copy, a copy of the object is constructed on another memory location.

If any changes happen in the copy object it does not affect the original object because both the objects point to separate memory locations.

To create the deep copy in Python we will use deepcopy() the function of the Python copy module.

let’s take the above example to understand the concept of the Python deep copy.

Example: Create a copy using deep copy:


import copy

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# copy of old_list
new_list = copy.deepcopy(old_list)

print("id of the old_list:- ", id(old_list))
print("id of the new_list:- ", id(new_list))

When you run the above program, the output will be:


id of the old_list:-  2522074712320
id of the new_list:-  2522074713024

As you can see in the above example, we have just created a deep copy of the object using deepcopy() the function. and to verify this we have just printed the id of the old_list and new_list.

Example: Adding a new nested object in the list using deep copy.


import copy

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# copy of old_list
new_list = copy.deepcopy(old_list)

# change the nested object of the old_list
old_list[2][1] = "Programming Funda"


print("Value of the old_list:- ", old_list)
print("Value of the new_list:- ", new_list)

When you run the above program, the output will be:


Value of the old_list:-  [[1, 2, 3], [4, 5, 6], [7, 'Programming Funda', 9]]
Value of the new_list:-  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In the above example, we have just created a new deep copy new_list. When we assign a new value to the, we can see the only old_list is modified because old_list and new_list point to the different objects on the different memory locations.

Conclusion

So, in this article, we have seen all Python deep and shallow copy using the Python copy module. deep and shallow copy is one of the most important questions asked by most of the interviewers to test known Python skills.

I hope you don’t have any confusion regarding deep and shallow copy in Python after reading this article.
if you really like this article, please share it with some who want to know about Python shallow and deep copy.

Check out our other Python tutorials.

Thanks for reading…

How to Limit Decimal Places in Python
Python Set Tutorial

Related Posts