del in Python – Remove Item from List based on Position

Spread the love

In this post, you will learn How to delete items from a list based on position.

del in Python –

If you know the position of an item in a list, you can remove it using del statement.

In [2]: companies = ['Google','Apple','Microsoft','Netflix', 'Facebook']

Let’s say you want to delete the first item from the list.

In [3]: del companies[0]

In [4]: companies
Out[4]: ['Apple', 'Microsoft', 'Netflix', 'Facebook']

You can remove any item from the list using del statement if you know the index of the item inn the list.

To delete the last item from the list

In [5]: del companies[-1]

In [6]: companies
Out[6]: ['Apple', 'Microsoft', 'Netflix']

Once you delete an item from a list using del statement you can not access that item again.

Rating: 1 out of 5.

Leave a Reply