The Basics When to Use the del Statement

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]

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]

Leave a Reply

Your email address will not be published. Required fields are marked *