May 2019
Beginner
528 pages
29h 51m
English
del StatementThe del statement also can be used to remove elements from a list and to delete variables from the interactive session. You can remove the element at any valid index or the element(s) from any valid slice.
Let’s create a list, then use del to remove its last element:
In [1]: numbers = list(range(0, 10))In [2]: numbersOut[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]In [3]: del numbers[-1]In [4]: numbersOut[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
The following deletes the list’s first two elements:
In [5]: del numbers[0:2]In [6]: numbersOut[6]: [2, 3, 4, 5, 6, 7, 8]
The following uses a step in the slice to delete every other element from the entire list: ...
Read now
Unlock full access