Python | Ways to remove particular list element

List is an important container and used almost in every code of day-day programming as well as web-development. The more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.

Let’s see the different ways of removing particular list element.

Method #1 : Using remove()
remove() can perform the task of removal of list element. Its removal is inplace and does not require extra space. But the drawback that it faces is that it just removes the first occurrence from the list. All the other occurrences are not deleted hence only useful if list doesn’t contain duplicates.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# element removal in list
# using remove() method
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : " 
                            + str(test_list1))
  
# using remove() to remove list element3
test_list1.remove(3)
  
# Printing list after removal
# only first occurrence deleted
print ("The list after element removal is : "
                           + str(test_list1))

chevron_right


Output:

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 4, 6, 3]

 
Method #2 : Using set.disard()
set.disard() can perform the task of removal of list element. Its removal is inplace and does not require extra space. List is first converted to set, hence other duplicates are removed and also list ordering is sacrificed. Hence not a good idea when we need to preserve ordering or need to keep duplicates.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# element removal in list
# using discard() method
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : " 
                             + str(test_list2))
  
# using discard() to remove list element 4
test_list2 = set(test_list2)
test_list2.discard(4)
  
test_list2 = list(test_list2)
  
# Printing list after removal
# removes element as distinct initially
print ("The list after element removal is : " 
                           + str(test_list2))

chevron_right


Output :

The list before element removal is : [1, 4, 5, 4, 5]
The list after element removal is : [1, 5]

 
Method #3 : Using Lambda Function + filter()
Lambda functions have always been a useful utility and hence can be used to perform tough task in just 1 liners. These can also perform this particular task. Drawback is that they are not inplace and require extra space or requires a overwrite. It actually constructs a new list, and filters out all of the required elements. It removes all the occurrences of element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# element removal in list
# using filter() + Lambda function
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list1))
  
# using filter() + Lambda function 
# to remove list element 3
test_list1 = list(filter(lambda x: x != 3, test_list1))
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list1))

chevron_right


Output:

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 4, 6]

 

Method #4 : Using List Comprehension
List comprehensions are easier method to perform the similar task as performed by lambda function. It has the same drawback of not being inplace and also requires extra space or overwrite. It is better in a way that filter() is not required to perform it. It removes all the occurrences of element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# element removal in list
# using List Comprehension
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list2))
  
  
# using List Comprehension
# to remove list element 4
test_list2 = [x for x in test_list2 if x != 4]
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list2))

chevron_right


Output :

The list before element removal is : [1, 4, 5, 4, 5]
The list after element removal is : [1, 5, 5]

 

Method #5 : Using pop()
Using pop method with list index to pop element out of list

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# element removal in list
# using pop() method
  
test_list1 = [1, 3, 4, 6, 3]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list1))
  
rmv_element = 4
  
# using pop()
# to remove list element 4
if rmv_element in test_list1:
    test_list1.pop(test_list1.index(rmv_element))
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list1))
  
# Added by Paras Jain(everythingispossible)

chevron_right


Output :

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 3, 6, 3]


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.



Improved By : everythingispossible