Ways to remove particular List element in Python
Last Updated :
17 Apr, 2023
List is an important container and is used almost in every code of day-day programming as well as web development. The more it is used, more is the required to master it and hence knowledge of its operations is necessary. Let’s see the different ways of removing particular list elements.
Method #1 : Using remove() remove() can perform the task of removal of list element. Its removal is in place 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 the list doesn’t contain duplicates.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 3 ]
test_list2 = [ 1 , 4 , 5 , 4 , 5 ]
print ( "The list before element removal is : "
+ str (test_list1))
test_list1.remove( 3 )
print ( "The list after element removal is : "
+ str (test_list1))
|
Output:
The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 4, 6, 3]
Time complexity: O(n), where n is the length of the list test_list1.
Auxiliary space complexity: O(1). The space used by the algorithm does not increase with the size of the input list
Method #2 : Using set.disard() set.disard()
It 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.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 3 ]
test_list2 = [ 1 , 4 , 5 , 4 , 5 ]
print ( "The list before element removal is : "
+ str (test_list2))
test_list2 = set (test_list2)
test_list2.discard( 4 )
test_list2 = list (test_list2)
print ( "The list after element removal is : "
+ str (test_list2))
|
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.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 3 ]
test_list2 = [ 1 , 4 , 5 , 4 , 5 ]
print ( "The list before element removal is : "
+ str (test_list1))
test_list1 = list ( filter ( lambda x: x ! = 3 , test_list1))
print ( "The list after element removal is : "
+ str (test_list1))
|
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.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 3 ]
test_list2 = [ 1 , 4 , 5 , 4 , 5 ]
print ( "The list before element removal is : "
+ str (test_list2))
test_list2 = [x for x in test_list2 if x ! = 4 ]
print ( "The list after element removal is : "
+ str (test_list2))
|
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
Approach:
- Define a list named test_list1 containing some elements.
- Print the initial list using the print() function.
- Assign the value 4 to the variable rmv_element. This value represents the element that we want to remove from the list.
- Use the if statement to check if the rmv_element is present in the list test_list1.
- If the element is present, use the pop() method to remove the first occurrence of the element from the list. The index() method is used to find the index of the element to be removed.
- Print the list after removal using the print() function.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 3 ]
print ( "The list before element removal is : "
+ str (test_list1))
rmv_element = 4
if rmv_element in test_list1:
test_list1.pop(test_list1.index(rmv_element))
print ( "The list after element removal is : "
+ str (test_list1))
|
Output :
The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 3, 6, 3]
Method #6: Using Recursion
Using Recursion to remove an element in a list
Python3
def remove_element(start, oldlist, newlist, element):
if start = = len (oldlist):
return newlist
if oldlist[start] = = element:
pass
else :
newlist.append(oldlist[start])
return remove_element(start + 1 , oldlist, newlist, element)
newlist = [ 1 , 2 , 3 , 29 , 2 , 13 , 421 , 31 ]
start = 0
element = 2
print ( 'The list Before removal: ' , newlist)
print ( 'The list After removal: ' , remove_element(start, newlist, [], element))
|
Output :
The list Before removal: [1, 2, 3, 29, 2, 13, 421, 31]
The list After removal: [1, 3, 29, 13, 421, 31]
The time complexity of the given remove_element function is O(n), where n is the length of the input list oldlist.
The auxiliary space complexity of the function is also O(n), where n is the length of the input list oldlist.
Please Login to comment...