Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jun 14, 2020
Embed
What would you like to do?
import copy
d={'Name':'karthi',
'Rollno':12,
'Marks':[100,90]
}
d1=d.copy()
print (d1) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90]}
#modifying immutable data types
d['Rollno']=5
print (d) #Output: {'Name': 'karthi', 'Rollno': 5, 'Marks': [100, 90]}
#changes are not reflected in copied dictionary.
print (d1) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90]}
# modifying mutable datatypes
d['Marks'].append(99)
print (d) #Output: {'Name': 'karthi', 'Rollno': 5, 'Marks': [100, 90, 99]}
#changes are reflected in copied dictionary also.
print (d1)#Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]}
d2=copy.deepcopy(d1)
print (d2) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]}
#modifying immutabe data types
d1['Rollno']=15
print (d1) #Output:{'Name': 'karthi', 'Rollno': 15, 'Marks': [100, 90, 99]}
#changes are not reflected in copied dictionary.
print (d2) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]}
# modifying mutable datatypes
d1['Marks'].append(101)
print (d1) #Output: {'Name': 'karthi', 'Rollno': 15, 'Marks': [100, 90, 99, 101]}
#changes are not reflected in deep copied dictionary.
print (d2)#Output:{'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment