I create a varibale temp = [10]
Then I put temp
into another list variable ls
After that, I use del temp
to delete the temp
variable
However, I check ls
and still find that [10]
is in ls
In my intuition, ls
should be printed as []
, but things don't go like that.
I want to ask the deep concept of del
in Python and maybe some information of copy by reference and copy by value in Python
My code:
temp = [10]
print('id of temp:', id(temp))
ls = [temp]
print('ls before del:', ls) # ls before del: [[10]]
del temp
print('ls after del:', ls) # ls after del: [[10]]
print('id of ls[0]:', id(ls[0]))
I dont suggest using del on list instead use pop or remove.
Remove
output
pop
output
Your Case