Using the del statement is relatively straightforward: it’s used to delete something. Often it’s used to remove an item from a list by referring to the item’s index rather than its value. For example, if you have the following list:
list = [4, 8, 2, 3, 9, 7]
- Python Programming – List Functions And Methods
- Python Programming – Common Tuple operations
- Python Programming – Creation, Initialization and Accessing The Elements In A Dictionary
And you want to remove the number 9, which has an index of 4 because it is the 5th item in the list (index starts at 0), you could do so using the del statement, like this:
del list[4]
So your list will now look like this:
list = [4, 8, 2, 9, 7]