How to remove item from list python?

In this python tutorial, we look at all the different methods you could use to remove items from lists in python. We also break down which methods are best suited for each use case.

This tutorial is a part of our initiative at Flexiple, to write sho…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this python tutorial, we look at all the different methods you could use to remove items from lists in python. We also break down which methods are best suited for each use case.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Content

Removing items from list python

Since lists are mutable, python comes built with a few list methods that can be used to remove items from lists. Some of these methods are pop(), remove(), clear(), etc. Although all these methods are used to remove items from lists in python they are built to cater to different use-cases, we discuss them in detail below. Apart from these methods, the del method can also be used to remove items in a list.

Remove item from list by Value

In this method, we remove items from the list using their value. And for this, we use the remove() list method. This method is quite straightforward however note that it only removes the first occurrence of the value and is case sensitive. Passing the value in the wrong case would return a ValueError.

Syntax of remove():

list.remove(value)

Here list refers to the name of the list.

Parameters:

value - The element that you want to remove.

Code to remove item from list by value:

list1 = ["Hire","Hire", "the", "top", 10, "python","freelancers"]

list1.remove("Hire")
print(list1)

#Output - ['Hire', 'the', 'top', 10, 'python', 'freelancers']

You can see that the first occurrence of "Hire" was deleted. You could also try breaking the code by using a lower case "hire".

Remove item from list by Index:

Since lists are ordered each item can be referred to using its index. And these indexes can also be used to remove item from list in python. For this, we use the pop() methods. Another use case of pop() is when you do not want to remove and return the particular item, in such cases pop() is used rather than remove(). Also, note that if no index is passed as a parameter, pop() removes and returns the last item.

Syntax of pop()

list.pop(index)

Here list refers to the name of the list.

Parameters:

index - Optional, the index of the item you want to remove.

Returns:

Return the item removed from the list, in case you do not pass an index the last value is returned.

Code to remove item from list by index:

list1 = ["Hire", "the", "top", 10, "python","freelancers"]

removed_item = list1.pop(0)
print(list1)
print(removed_item)

#Output - ['the', 'top', 10, 'python', 'freelancers']
#Output - "Hire"

Remove item from list using del:

del is another way to remove items from list in python. Although this is not a list method it does come with some unique use cases. Similar to pop(), del also removes items using their index however it can be used to remove multiple items at a time.

Syntax of del:

del object_name

Passing the name of the list followed by the index or an index range after del will remove the items from the list in python.

Code to remove items using del

list1 = ["Hire", "the", "top", 10, "python","freelancers"]

del list1[0]
print(list1)

#Output - "['the', 'top', 10, 'python', 'freelancers']"

Similarly, if you are looking to remove multiple items from a list, adding an index range would help

list1 = ["Hire", "the", "top", 10, "python","freelancers"]

del list1[0:4]
print(list1)

#Output - ['python', 'freelancers']

Limitations and Caveats:

  • While trying to remove item from list in python using remove() a ValueError is returned if the value passed as a parameter cannot be found.
  • The pop() only allows int to be passed as a parameter, and an IndexError is returned if the index is out of range.


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-05-22T11:16:56+00:00) How to remove item from list python?. Retrieved from https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/

MLA
" » How to remove item from list python?." hrishikesh1990 | Sciencx - Saturday May 22, 2021, https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/
HARVARD
hrishikesh1990 | Sciencx Saturday May 22, 2021 » How to remove item from list python?., viewed ,<https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/>
VANCOUVER
hrishikesh1990 | Sciencx - » How to remove item from list python?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/
CHICAGO
" » How to remove item from list python?." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/
IEEE
" » How to remove item from list python?." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/. [Accessed: ]
rf:citation
» How to remove item from list python? | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/05/22/how-to-remove-item-from-list-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.