Signup/Sign In

Python Deep copy - What is it?

Posted in Technology   LAST UPDATED: OCTOBER 22, 2019

    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.

    Author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    PythonPython Deep Copy
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS