Deep Copying
Deep copying creates a completely independent copy of the original list, including all nested structures. This means that changes to nested lists or objects will not affect the copy. The copy module provides a deepcopy() function that makes a complete copy of the list, including all nested mutable objects. Here, changes in the nested lists of one list do not affect the other list, which is the main advantage of deep copying.
import copy
list1 = [[1, 2], [3, 4]]
list2 = copy.deepcopy(list1)
list2[0].append(5)
print(list1) # [[1, 2], [3, 4]]
print(list2) # [[1, 2, 5], [3, 4]]
Page of .
Leave a comment
You must be logged in to leave a comment.
Log in or
sign up to join the discussion.
Comments (0)