Try it here
Subscribe
deepcopy() function

Python deepcopy

python_deepcopy_

A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements.

The deep copy creates independent copy of original object and all its nested objects.

>>> import copy
>>> old=[[1,2],[2,3],[3,4]]
>>> old
[[1, 2], [2, 3], [3, 4]]
>>> new=copy.deepcopy(old)
>>> new
[[1, 2], [2, 3], [3, 4]]
>>> old[1][1]='AB'
>>> old
[[1, 2], [2, 'AB'], [3, 4]]
>>> new
[[1, 2], [2, 3], [3, 4]]
>>> 

In the above program, when we assign a new value to old list, we can see only the old list is modified. This means, both the old list and the new list are independent. This is because the old list was recursively copied, which is true for all its nested objects.

Also see Shallow Copy

Writer profile pic

Anmol on May 08, 2020 at 01:04 am


This article is contributed by Anmol. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.