Search This Blog

170 Python Other topics

 13 Other Topics:

 Python Shallow Copy, Deep Copy, Copy using =Operator
#Copy using = operator
list1 = [[1,2],[3,4],[5,6]]

list2 = list1 # Creates variable list2 referring Object list1

print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))

list1[1][1]=10

print('list1 after modification:', list1)
print('list2 after modification:', list2)
print('ID of list2:', id(list2))
O/p:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 82228360
list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 10], [5, 6]]
ID of list2: 82228360

list1 and list2 has same ID. If we modify values any, changes will appear in the other also.

To overcome this we have,
Shallow copy 
Deep copy.

Copy module is used for both shallow and deep copy.

Shallow Copy:
# shallow copy
import copy
list1 = [[1,2],[3,4],[5,6]]
list2 = copy.copy(list1) #list2 is shallow copy of list1. ( it is created as new object ) 
          # which has reference of the original elements of list1.                          
print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))

list1[1][1]=10

print('list1 after modification:', list1)
print('list2 after modification:', list2)
print('ID of list2:', id(list2))

list1.append([7,8])

print('list1 after sublist addition:', list1)
print('list2 after sublist addition:', list2) # appended sublist wont reflect here
Output:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 93070824

list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 10], [5, 6]]
ID of list2: 93070696

list1 after sublist addition: [[1, 2], [3, 10], [5, 6], [7, 8]]
list2 after sublist addition: [[1, 2], [3, 10], [5, 6]]



Deep Copy:
# Deep copy
import copy
list1 = [[1,2],[3,4],[5,6]]
list2 = copy.deepcopy(list1) # list2 is deep copy of list1. ( it is created as new object)
# and recursively adds the copies of nested objects present in the original elements.
# means list2 elements wont get effected when list1 elements modified
print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))
print('ID of list2:', id(list2))

list1[1][1]=10
print('list1 after modification:', list1)
print('list2 after modification:', list2)


list1.append([7,8])
print('list1 after sublist addition:', list1)
print('list2 after sublist addition:', list2)
Output:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 86296296
ID of list2: 86295816

list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 4], [5, 6]]


list1 after sublist addition: [[1, 2], [3, 10], [5, 6], [7, 8]]
list2 after sublist addition: [[1, 2], [3, 4], [5, 6]]

No comments:

Post a Comment