Related Articles

Related Articles

Python | Remove given element from the list
  • Last Updated : 20 May, 2019

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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :