The del statement can be used to remove an item from a list by referring to its index, rather than its value. For example, if you have a list with five values, like this:
a = [1, 2, 3, 4, 5]
- The Basics: When to Use the del Statement
- Python Lists and Tuples
- Python Programming – List Functions And Methods
and you want to remove the second listed value (2), you can use del to do so. The second listed value has an index of 1, so to remove the value, the del syntax would need to look like this:
del a[1]
Now a will look like this:
a = [1, 3, 4, 5]