I am a novice when it comes to using Python language. I want to understand why removing an item from a dictionary using the del
keyword is impossible when someone references the item using a variable. It seems the del
keyword only works if one uses the square bracket format i.e del dictionary[key
] why is this so? I have provided a code snippet to help in understanding my question and guidance when it comes to answering the question (don't forget to mention the concept of id).
>>> personal_details = {'f_name': 'Imani', 'l_name': 'Ritcher', 'age': 25}
>>> first_name = personal_details['f_name']
>>> id(first_name)
139769037520176
>>> id(personal_details['f_name'])
139769037520176
>>> del first_name
>>> personal_details
{'f_name': 'Imani', 'l_name': 'Ritcher', 'age': 25}
>>> first_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'first_name' is not defined
>>> del personal_details['f_name']
>>> personal_details
{'l_name': 'Ritcher', 'age': 25}
>>>
TL;TR In Python variables are technically references to objects, rather than the objects themselves.
When you create first_name
(first_name = personal_details['f_name']
), you are not creating any new object but a reference to already existing object (being the string Imani
). Because f_name
element in personal_details
dict is also a reference to Imani
, then after your assignment both first_name
and personal_details['f_name']
point to the same object in memory. And that's why id()
returns the same value for both variables.
Deleting is opposite. Use of del
is not removing any object, but again, it deletes the reference that was created before. But the target object remains unaffected and that's why you can safely remove i.e. first_name
and dict element is unaffected (and vice versa) even if they pointed to the same object. Finally, when you remove both references, then Imani
become orphan and then it will be garbage collected by Python at some point.