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.]
# 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)) |
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
# 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)) |
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.
# 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)) |
original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [8, 1, 2, 4]
Method #4: Using list comprehension
# 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]
Recommended Posts:
- Python | Ways to remove particular list element
- Python | Remove given element from list of lists
- Python | Remove particular element from tuple list
- Python | Remove element from given list containing specific digits
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Replace elements in second list with index of same element in first list
- Python | Remove None values from list
- Python | Remove Duplicates from a List
- Python | Remove last K elements of list
- Python | Indices list of matching element from other list
- Python | Ways to remove duplicates from list
- Python | Remove last character in list of strings
- Python | Remove duplicates from nested list
- Python | Remove additional spaces in list
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.



