I have question with dictionary copy method for example lets say i have
>> d = {\'pears\': 200, \'apples\': 400, \'oranges\': 500, \'bananas\': 300}
&
The reason is that you performed an assignment operation which replaces values, not a value modification operation.
copy_dict = d.copy()
Caused a new dict
to be created and its keys/values were initialized from d
. You noted the important point - these are separate objects with different ids that just happen to reference the same keys and values.
d['pears'] = 700
removed the reference to 200
from d['pears']
and added a reference to 700
. This reassignment was made on the d
object itself so it naturally would not be seen by other dicts that were simply initialized by the same keys and values.
In contrast, supposed you had simply assigned the original dict to a second variable
copy_dict = d
Here, since both d
and copy_dict
reference the same dict
, reassignment on that dict
would be seen by both variables because they reference the same object.