Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created June 14, 2020 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/5d7f123c592c7daaa275e3f67dd801a7 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/5d7f123c592c7daaa275e3f67dd801a7 to your computer and use it in GitHub Desktop.
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