I have a question with a way to copy a dictionary for example, let's say I have
>> d = {'pears': 200, 'apples': 400, 'oranges': 500, 'bananas': 300} >> copy_dict = d.copy()
Now, if I check the id of both d and copy_dict, both of them are different
>> id(d) o/p = 140634603873176 >> id(copy_dict) o/p = 140634603821328
but if I check the identifier of objects in dictionaries, then they have the same meaning id (d ['pears']) = id (copy_dict ['pears'])
>> id(d['pears']) o/p = 140634603971648 >> id (copy_dict['pears']) o/p = 140634603971648
All objects in the new dict are references to the same objects as the original dict.
Now, if I change the value of the pear key to d, there are no changes in copy_dict in the same way, and when I check the identifier, id (d ['pears'])! = Id (copy_dict ['pears'])
>> d['pears'] = 700 >> print copy_dict['pears'] o/p = 200
My question is: if the objects in the new dict are references to the same objects as the original dict, why did the value of the new dict not change when the value in the original dictionary was changed and how did Python immediately change the id as soon as it changed the value?
Could you give me a full description of the differences between a deep copy and a shallow copy?
source share