remove in python – Remove an Item By Value from List

Spread the love
\

In this post, you will learn How to remove an item by value from a list.

remove in python –

Sometimes you don’t know the index of the item that you want to delete from the list. But if you know the value of the item that you want to delete then you can use the remove method in python.

Syntax of remove in List-

list.remove(item)

The remove method in list takes a single argument the value that you want to remove from the list.

Remove an Item from The List –

Let’s say that you want to remove Facebook from the list of companies.

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

In [18]: companies.remove('Facebook')

In [19]: companies
Out[19]: ['Google', 'Apple', 'Microsoft', 'Netflix']

Remove from list when duplicate item exists –

If you list contains duplicate items, then remove will only delete the first item from the list and leave the remaining as it is.

In [20]: fruits = ['Mango','Apple','Apple','Apple','Banana']

In [21]: fruits.remove('Apple')

In [22]: fruits
Out[22]: ['Mango', 'Apple', 'Apple', 'Banana']

Rating: 1 out of 5.

Leave a Reply