How does del operator work in list in python?

Viewed 6136

I have read the python docs for list and how the del operators works, but I need explanation for the following behavior

In this case, c and l points to the same object(list), so doing changes on one affects the other, but deleting one does not delete the object. So what happens here? Is it just the pointer to the list object is lost?

>>> l = [1,2,3]
>>> c = l
>>> c.append(4)
>>> c
[1, 2, 3, 4]
>>> l
[1, 2, 3, 4]
>>> del c
>>> l
[1, 2, 3, 4]
>>> c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined

Deletion by slice operation

>>> l
[1, 2, 3, 4]
>>> del l[::2]
>>> l
[2, 4]

l[::2] returns the new list. but del l[::2] does in-place deletion. So in this case, is not a new list being returned? What exactly is happening here?

1 Answers
Related