In Python, a list is an ordered collection of items. In this blog, we will understand how to remove element from list python. so let’s get started.
Remove element from list python
There are several ways to remove an element from a list in Python. In this session, we will learn each of the methods of list removal with examples.
Remove element from list python using the remove method
The remove() method is used to remove the first occurrence of the element from the list. If the element is not found in the list then it will raise a ValueError
.
Let’s try to remove 3 from list l.
l = [1,2,3,4,5,6,1,2] l.remove(3) print(l)
As shown in the below screenshot, the remove method removes element 3 from list l.
Remove element from list python using the pop method
The pop()
method removes the element from the list at the specified index. If no index is provided then it removes and returns the last element of the list.
l = [1,2,3,4,5,6,1,2] removed_item = l.pop(2) print(removed_item) print(l)
The above code removes the element at the 2nd index which is number 3.
Remove element from list python using the slice method
A user can use the slice method removes the element at the specified index. Check the below example
l = [1,2,3,4,5,6,1,2] del l[2] print(l)
The above codes delete the elements from index 2.
Remove the element from the list example
In this session, we will see the various example of list manipulations.
Remove element from the python list by index
A user can either use the pop() or slice method to remove an element by index from the python list. Below is an example of the pop method to remove the element at the 3rd index.
l = [1,2,3,4,5,6,1,2] removed_item=l.pop(3) print(removed_item) print(l)
The above code removes element 4 from list l
Below is the code to remove the element by index using the slice method.
l = [1,2,3,4,5,6,1,2] del l[4] print(l)
Python remove elements from the list by value
The python remove() method, removes the elements from the list by value. Please note that the remove method will only delete the first occurrence of an element.
l = [1,2,3,4,5,6,1,2] l.remove(2) print(l)
The above code only removes the first occurrence of 2.
If we wish to remove all occurrences of an element from the list then he has to use loops. Check the below example
l = [1,2,3,4,5,6,1,2] for item in l: if item ==2: l.remove(item) print(l)
Remove elements from the python list based on the condition
A user can extend the python remove() function to remove the elements based on certain conditions. Let’s say we wish to delete all even items from the below list l
l = [1,2,3,4,5,5,6,7,8,9,10,11,12] for item in l: if item % 2 == 0: l.remove(item) print(l)
The above code removes all the even items from list l.In a similar manner, a user can apply many conditions based on his requirement to remove elements from the list.
Remove all elements from the list python
Python provides a method called clear() which reassigns the list to an empty list. The clear() method removes all the elements from the list l.
l = [1,2,3,4,5,5,6,7,8,9,10,11,12] l.clear()
Alternatively, a user can also use the reassignment method to assign the list to an empty list.
l = [1,2,3,4,5,5,6,7,8,9,10,11,12] l = [] print(l)
The above code assigns the list l to the empty list
Python remove the first element from the list
Many times a user wants to delete only the first element from the list. He can achieve this by either using pop() or the index method.
Below is the code to remove the first element using the pop() method.
l = [1,2,3,4,5,6,7] l.pop(0) print(l)
The above code removes the element from index 0, which is the first element in the list.
Below is the code for the removal of the first element from the list using the index method.
l = [1,2,3,4,5,6,7] del l[0] print(l)
Conclusion
I hope you have liked this tutorial about remove elements from the list using python. Please do let me know if you need more input.