Python | Remove given element from the list

Given a list, write a Python program to remove the given element (list may have duplicates) from given list. There are multiple ways we can do this task in Python. Let’s see some of Pythonic ways to do this task.

Method #1: Using pop() method [Remove given element found first.]

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 
    
remove = 9
    
# using pop() 
# to remove list element 9
if remove in list1: 
    list1.pop(list1.index(remove)) 
    
# Printing list after removal 
print ("List after element removal is : "  + str(list1)) 

chevron_right


Output:



original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 9, 2, 9]

 
Method #2: Using remove() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 remove() to remove list element 9
list1.remove(9
  
  
# Printing list after removal 
print ("List after element removal is : "  + str(list1)) 

chevron_right


Output:

original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 9, 2, 9]

Now, let’s see the ways to remove all occurrence of given element.

Method #3: Using set

Since the list is converted to set, all duplicates are removed, but the ordering of list cannot be preserved.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 discard() method to remove list element 9
list1 = set(list1) 
list1.discard(9
    
list1 = list(list1) 
  
  
# Printing list after removal 
print ("List after element removal is : "  + str(list1)) 

chevron_right


Output:

original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [8, 1, 2, 4]

 
Method #4: Using list comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output:

original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.