Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does del operator work in list in python?

Tags:

python

list

del

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?

like image 565
eagertoLearn Avatar asked Dec 30 '13 20:12

eagertoLearn


People also ask

How does Del work in Python list?

The del statement works by unbinding the name, removing it from the set of names known to the Python interpreter. If this variable was the last remaining reference to an object, the object will be removed from memory. If, on the other hand, other variables still refer to this object, the object won't be deleted.

How do you use Del in a list?

The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method. So essentially, this operator takes the item's index to be removed as the argument and deletes the item at that index.

Does Del work with lists?

With del, we specify a list, dictionary or other collection. We pass an index or key we want to remove. On lists, we can remove slices (ranges of elements) at once.

What is the difference between remove () and Del () in Python list?

The remove() method doesn't return any value. pop() returns deleted value. The del keyword can delete the single value from a list or delete the whole list at a time. At a time it deletes only one value from the list.


1 Answers

l and c are bound to the same object. They both are references to a list, and manipulating that list object is visible through both references. del c unbinds c; it removes the reference to the list.

del l[::2] removes a specific set of indices from the list, you are now operating on the list object itself. You are not unbinding l, you are unbinding indices inside of the list.

You can compare this with retrieving and setting values as well. print c is different from print c[::2] and c = something is different from c[::2] = something; the first of both examples accesses just the list object, or assign a new value to c, the latter examples retrieve a slice of values or set new values to the sliced indices.

Under the hood, del c removes the name c from the dictionary handling all variables (globals() gives you a reference to this dictionary). del l[::2] calls the __delitem__ special method on the list, passing in a slice() object.

like image 137
Martijn Pieters Avatar answered Sep 20 '22 05:09

Martijn Pieters