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]
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.
Recommended Posts:
- Python | Remove given element from list of lists
- Python | Remove element from given list containing specific digits
- Python | Ways to remove particular list element
- Python - Remove first element of list
- Python | Remove particular element from tuple list
- Python | Remove random element from list
- Python | Remove duplicates based on Kth element tuple list
- Python - Remove rear element from list
- Remove all the occurrences of an element from a list in Python
- Python - Remove Tuples from the List having every element as None
- Python | Remove all values from a list present in other list
- Python - Remove empty List from List
- Python - Remove given character from first element of Tuple
- Python | Remove trailing empty elements from given list
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Sort given list by frequency and remove duplicates
- Python | Remove repeated sublists from given list
- Python | Remove given character from Strings list
- Python | Remove List elements containing given String character
- Python | Slicing list from Kth element to last element
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.

