Python del Statement to delete objects

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]

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]

Leave a Reply

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