Remove an Item from a List
We will use a different method to Remove Elements from the List in Python:
- Using Python remove()
- Using Python del
- Using Python List comprehension
- Using Python pop()
- Using Python discard()
- Using Python filter()
- Using Python List Slicing
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) # using remove() lst.remove( 'Orchids' ) print ( "After deleting the item :" , lst) |
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) # using del statement # to delete item (Orchids at index 1) # from the list del lst[ 1 ] print ( "After deleting the item :" , lst) |
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
# Python program to remove given element from the list list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ] # Printing initial list print ( "original list : " + str (list1)) # using List Comprehension # to remove list element 9 list1 = [ele for ele in list1 if ele ! = 9 ] # Printing list after removal print ( "List after element removal is : " + str (list1)) |
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) # using pop() to delete item # ('Orchids' at index 1) from the list a = lst.pop( 1 ) print ( "Item popped :" , a) print ( "After deleting the item :" , lst) |
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
# Python program to remove given element from the list lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' , 'Lily' , 'Carnations' ] print ( "Original List is :" , lst) # using discard() method to remove list element 'orchids' lst = set (lst) lst.discard( 'Orchids' ) # Converting set back to list 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
# Python program to remove given element from the list lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' , 'Lily' , 'Carnations' ] print ( "Original List is :" , lst) # using discard() method to remove list element 'orchids' 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] |
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) # itertools.filterfalse() to filter out all occurrences of 9 from the list lst_filtered = list (itertools.filterfalse( lambda x: x = = 9 , lst)) print ( "List after element removal is :" , lst_filtered) #this code is contributed by Jyothi pinjala. |
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:
How to Remove an Item from the List in Python
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.