Technology3 min read

Python Deep copy - What is it?

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python Deep Copy

Summarize this article using AI

In one of the previous posts, we understood what shallow copy is and how it works in different situations. In this post, we will understand what deep copy is and how it works. Shallow copy and deep copy related questions are usually asked in interviews to test the candidate for the depth of knowledge that they have. In such situations, it is important to give proper explanation along with relevant examples.

Without further delay, let us begin. In simple words, the process of copying objects occurs repeatedly in the case of using the deepcopy method of the copy module.

When the elements of the original object are copied using the deepcopy method, a new object is created. In this newly created object, references of elements from the original object are repeatedly populated, i.e a copy of the entire object is actually placed inside the newly created object. This means changes made to the original object's elements will not reflect in the newly created object, in contrast to shallow copy, in which changes made to the original object are reflected in the new object.




Time for an example

The below example shows that the IDs of both objects are different since they don't share the references.

import copy

my_list_old = [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a']]
my_list_new = copy.deepcopy(my_list_old)

print('The old List is', my_list_old)
print('ID of the Old List:', id(my_list_old))

print('The new List:', my_list_new)
print('ID of the new List:', id(my_list_new))

Output:

The old List is [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a']]
ID of the Old List: 2516698103880
The new List: [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a']]
ID of the new List: 2516698116872

The below example demonstrates the fact that changing values in the original object will not change the newly created object that contains a copy of the original object elements.

import copy

my_list_old = [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a']]
my_list_new = copy.deepcopy(my_list_old)

my_list_old.append(["I", "love", "Studytonight"])

print('The old List is', my_list_old)
print('The new List:', my_list_new)

Output:

The old List is [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a'], ['I', 'love', 'Studytonight']]
The new List: [[1, 2, 's'], [3, 8, 9.78], [7, 8, 'a']]

Below is a visualization of the deep copy method that will make your understanding of deep copy solid.

Python deep copy




Conclusion

In this post, we understood what deep copy method is, and how it works. Don't forget to share with us in the comments about how you used the deep copy method in your Python coding.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next