Showing posts with label python_shallow_copy. Show all posts
Showing posts with label python_shallow_copy. Show all posts

Sep 17, 2019

Python Shallow vs Deep Copy

"""
1. Copy by Reference
2. Shallow Copy
3. Deep Copy
"""

import copy

#Copy Ref
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9

print('----------')
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print('Copy Ref - Old List:', old_list)
print('Copy Ref - New List:', new_list)
print('----------')

#Output
#ID of Old List: 140672073880512
#ID of New List: 140672073880512
#Copy Ref - Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Copy Ref - New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


#Shallow Copy
##############
#A shallow copy creates a new object which stores the reference of the original elements.

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)

print("Shallow Copy Old list:", old_list)
print("Shallow Copy New list:", new_list)
print('----------')

#Output:
#Shallow Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#Shallow Copy - append
######################
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list.append([10,11,12])
print("Shallow Copy add Old list:", old_list)
print("Shallow Copy add New list:", new_list)
print('----------')

#Output:
#Shallow Copy add Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy add New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]


#Shallow Copy - nested update
###########################
#Existing elements will get updated - since it has reference to original elements

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list[1][1] = 400
print("Shallow Copy nested Old list:", old_list)
print("Shallow Copy nested New list:", new_list)
print('----------')

#Output:
#Shallow Copy nested Old list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Shallow Copy nested New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]


#Deep Copy###########
#It makes complete copy of elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.deepcopy(old_list)
new_list[1][1] = 400
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))

print("Deep Copy Old list:", old_list)
print("Deep Copy New list:", new_list)

#Output:
#ID of Old List: 140672073879792
#ID of New List: 140672073880992
#Deep Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Deep Copy New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]



Jan 22, 2019

Python Shallow Vs Deep Copy

import copy

#Shallow Copy
#   copy.copy(x)
#   A shallow copy creates a new object which stores the reference of the original elements.
#   A shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. 

#Deep Copy
#   copy.deepcopy(x)
#   A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements.


print ' --------- Copy by Ref ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = dic1

print dic1
print dic2

print 'Updating/Appending score 50'
dic1['Scores'].append(50) #update existing

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

#Both dic1 & dic2 point to same memory locations
print id(dic1) #46138688
print id(dic2) #46138688

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

print ' --------- Shallow Copy ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = copy.copy(dic1)

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print "Memory locations of dic1 & dic2 respectively"
#Both dic1 & dic2 point to different memory location
print id(dic1) #57641696
print id(dic2) #57641840

print 'Updating/Appending score 50'
#Updating the existing will 
dic1['Scores'].append(50) #update existing

#Both dic1 & dic2 point to different memory locations
print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

#Shallow Copy -> Internal data points to the same memory location
print "Memory locations of dic1['Scores'] & dic2['Scores'] respectively"
print id(dic1['Scores']) #52223472
print id(dic2['Scores']) #52223472

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}

print ' --------- Deep Copy ------ '
dic1 = {'StudnetId': 'test1', 'Scores': [60,70,80,90,85,45]}
dic2 = copy.deepcopy(dic1)

#Both dic1 & dic2 point to different memory locations
print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print id(dic1) #54035776
print id(dic2) #54037504

print 'Updating/Appending score 50'
dic1['Scores'].append(50) #update existing

print dic1 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}

print 'Adding new field Age'
dic1['Age'] = 25

print dic1 #{'Age': 25, 'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45, 50]}
print dic2 #{'StudnetId': 'test1', 'Scores': [60, 70, 80, 90, 85, 45]}