How to Remove an Item from the List in Python
Last Updated :
21 Dec, 2023
Python Lists have various built-in methods to remove items from the list. Apart from these, we can also use different methods to remove an element from the list by specifying its position. This article will examine various Python methods for removing items from lists.
Example
Input: ['Rose',' Lily', 'Lotus', 'Sun', 'Sunflower']
Delete: 'Sun'
Output: ['Rose',' Lily', 'Lotus', 'Sunflower']
Explanation: In this, we have removed the 'Sun' element from the given list.
Remove an Item from a List
We will use a different method to Remove Elements from the List in Python:
1. Remove Elements from the List using remove()
We can remove elements from the list by passing the value of the item to be deleted as the parameter to remove the () function.
Python3
lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,
'Lily' , 'Carnations' ]
print ( "Original List is :" , lst)
lst.remove( 'Orchids' )
print ( "After deleting the item :" , lst)
|
Output
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
2. Remove Element from the List using del()
We can remove elements from the list using Del(). The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted.
Python3
lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,
'Lily' , 'Carnations' ]
print ( "Original List is :" , lst)
del lst[ 1 ]
print ( "After deleting the item :" , lst)
|
Output
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
3. Remove Element from the List using List Comprehension
We can remove elements from the list while iterating. In this method, we are using list comprehension. Here, we are appending all the elements except the elements that have to be removed.
Python3
list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]
print ( "original list : " + str (list1))
list1 = [ele for ele in list1 if ele ! = 9 ]
print ( "List after element removal is : " + str (list1))
|
Output
original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]
4. Remove Element from the List using pop()
We can remove elements from the list using pop(). The pop() is also a method of listing. We can remove the element at the specified index and get the value of that element using pop().
Python3
lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,
'Lily' , 'Carnations' ]
print ( "Original List is :" , lst)
a = lst.pop( 1 )
print ( "Item popped :" , a)
print ( "After deleting the item :" , lst)
|
Output
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
Item popped : Orchids
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
5. Remove Element from the List Using discard()
We can remove elements from the list using discard(). In this method, we convert a list into a set and then delete an item using the discard() function. Then we convert the set back to the list.
Python3
lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,
'Lily' , 'Carnations' ]
print ( "Original List is :" , lst)
lst = set (lst)
lst.discard( 'Orchids' )
lst = list (lst)
print ( "List after element removal is :" , lst)
|
Output:
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
List after element removal is : ['Lily', 'Carnations', 'Iris', 'Rose', 'Lavender']
Note: Since the list is converted to a set, all duplicates will be removed and the ordering of the list cannot be preserved.
6. Remove Element From the List Using filter()
We can remove elements from the list using filter(). In this method, we filter the unwanted element from the list using the filter() function.
Python3
lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,
'Lily' , 'Carnations' ]
print ( "Original List is :" , lst)
lst1 = filter ( lambda item: item! = 'Orchids' ,lst)
print ( "List after element removal is :" , list (lst1))
|
Output
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
List after element removal is : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
7. Remove Element from the List Using Slicing
We can remove elements from the list using slicing. This method creates a new list by slicing the original list and concatenating the parts that do not include the removed element.
Python3
my_list = [ 1 , 2 , 3 , 4 , 5 ]
my_list = my_list[: 2 ] + my_list[ 3 :]
print (my_list)
|
Output:
[1, 2, 4, 5]
8. Remove Element from the List Using Itertools
We can remove elements from the list using itertools. The code uses the itertools.filterfalse() function to remove all occurrences of the number 9 from a given list.
It creates a lambda function to check if an element is equal to 9 and applies the filter to the list. The resulting filtered list is printed as the output.
Python3
import itertools
lst = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]
print ( "Original List is :" , lst)
lst_filtered = list (itertools.filterfalse( lambda x: x = = 9 , lst))
print ( "List after element removal is :" , lst_filtered)
|
Output
Original List is : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]
In this article, we have discussed various methods to remove an item from the list. There are a total of 8 methods mentioned in this article. Removing an element from lists can be done using built-in functions but we have also used non-conventional methods.
Similar Reads:
Please Login to comment...