Generally, when we delete something we are removing references to that object.
>>> c = 42
>>> print (c)
42
>>> del(c)
>>> print (c)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print (c)
NameError: name 'c' is not defined
>>>
In the case of a list element, we would be removing the reference to that index…
>>> a = [1,2,3,4,5]
>>> del(a[4])
>>> a[4]
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
a[4]
IndexError: list index out of range
>>> a
[1, 2, 3, 4]
>>>
Note that if the item is not at the end of the list, the reference still exists at that index, but with the values to the right shifted to fill the gap. The last index reference will be gone.
>>> del(a[1])
>>> a
[1, 3, 4]
>>> del(a[1])
>>> a[1]
4
>>>
In the case of dictionaries, we would be removing the reference to the key-value pair…
>>> b = { 'one': 1, 'two': 2, 'three': 3 }
>>> b
{'three': 3, 'one': 1, 'two': 2}
>>> del(b['two'])
>>> b['two']
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
b['two']
KeyError: 'two'
>>> b
{'three': 3, 'one': 1}
>>>
We can delete a element from a set…
>>> s = {5, 8, 13, 21}
>>> del(a[1])
>>> a
[1, 3, 4]
>>>
Like lists, the last index is gone.
Tuples are immutable so we cannot delete elements.
>>> t = (3, 6, 9, 12)
>>> del(t[0])
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
del(t[0])
TypeError: 'tuple' object doesn't support item deletion
>>>
We can, however, delete a tuple (as with any other object)…
>>> del(t)
>>> t
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
t
NameError: name 't' is not defined
>>> del(s)
>>> s
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
s
NameError: name 's' is not defined
>>> del(b)
>>> b
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
b
NameError: name 'b' is not defined
>>> del(a)
>>> a
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
a
NameError: name 'a' is not defined
>>>
Since functions are also objects, this means we can delete them, too…
>>> def foo():
return "Foo"
>>> foo()
'Foo'
>>> del(foo)
>>> foo()
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
foo()
NameError: name 'foo' is not defined
>>>
From the above we can see that del()
is a far-reaching function of the standard library that is not tied to any one or more classes. This can lead to errors if not implemented carefully, and for this reason should be the last thing reached for. Better that we use the class methods, when they are available. This is where object.remove()
comes into play.
>>> a = [1,2,3,4,5]
>>> a.remove(4)
>>> a
[1, 2, 3, 5]
>>> a = [1,2,3,4,5] + [1,2,3,4,5]
>>> a.remove(4)
>>> a
[1, 2, 3, 5, 1, 2, 3, 4, 5]
>>>
Note that only the first occurrence of 4
is removed. To remove all the occurrences, we need to write a while loop…
>>> a = [1,2,3,4,5] + [1,2,3,4,5]
>>> while 4 in a:
a.remove(4)
>>> a
[1, 2, 3, 5, 1, 2, 3, 5]
>>>
To remove multiple adjacent elements in a list we turn back to del()
…
>>> del(a[3:6])
>>> a
[1, 2, 3, 3, 5]
>>>
Care must be taken when writing code of this nature.
Dictionaries (dict objects) do not have a remove method.
>>> b = { 'one': 1, 'two': 2, 'three': 3 }
>>> b.remove('one')
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
b.remove('one')
AttributeError: 'dict' object has no attribute 'remove'
>>>
Sets do have a remove method…
>>> s = {5, 8, 13, 21}
>>> s.remove(8)
>>> s
{13, 5, 21}
>>>
It goes without saying that tuples do not.
Lists, dictionaries and sets also have a pop()
method which lets us capture the removed item for further use. That will come up, if it hasn’t already.
>>> c = b.pop('two')
>>> c
2
>>> b
{'three': 3, 'one': 1}
>>>