Python | Remove given element from the list (original) (raw)

Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let’s see some of the Pythonic ways to do this task.

Example:

Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9 is Removed from the List.

Remove element from a list using pop() method

The pop() is also a method of listing. We can remove the element at the specified index and get the value of that element using pop(). Here, we first find the index position of 9, and then we will pass the index to pop() function.

Python3

list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "original list : " + str (list1))

remove = 9

if remove in list1:

`` list1.pop(list1.index(remove))

print ( "List after element removal is : " + str (list1))

Output:

original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 9, 2, 9]

Time Complexity: O(n)
Auxiliary Space: O(n)

Delete an element from a list using remove() method

We can remove an item from the list by passing the value of the item to be deleted as the parameter to remove() function.

Python3

list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "original list : " + str (list1))

list1.remove( 9 )

print ( "List after element removal is : " + str (list1))

Output:

original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 9, 2, 9]

Time Complexity: O(n)
Auxiliary Space: O(n)

Remove an element from a list using list comprehension

In this method, we are using list comprehension. Here, we are appending all the elements except the elements that have to be removed.

Python3

list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "original list : " + str (list1))

list1 = [ele for ele in list1 if ele ! = 9 ]

print ( "List after element removal is : " + str (list1))

Output:

original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]

Time Complexity: O(n)
Auxiliary Space: O(n)

Delete an item from a list using the del

The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted.

Python3

lst = [ 'Iris' , 'Orchids' , 'Rose' , 'Lavender' ,

`` 'Lily' , 'Carnations' ]

print ( "Original List is :" , lst)

del lst[ 1 ]

print ( "After deleting the item :" , lst)

Output:

Original List is : [‘Iris’, ‘Orchids’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’] After deleting the item : [‘Iris’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]

Time Complexity: O(n) where n is the elements in the list
Auxiliary Space: O(n), where n is the length of the list

Remove the given element from a list using a set

Since the list is converted to set, all duplicates are removed, but the ordering of the list cannot be preserved.

Python3

list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "original list : " + str (list1))

list1 = set (list1)

list1.discard( 9 )

list1 = list (list1)

print ( "List after element removal is : " + str (list1))

Output:

original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [8, 1, 2, 4]

Using a combination of list comprehension and filter with help of Lambda Function:

This solution uses filter to create a new list containing all the elements from list that are not equal to remove, and then assigns the resulting list to the result variable. This solution is similar to the solution using list comprehension and filter, but it is more concise and easier to read.

Python3

lst = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "Original list:" , lst)

remove = 9

result = list ( filter ( lambda x: x ! = remove, lst))

print ( "List after element removal:" , result)

Output

Original list: [1, 9, 8, 4, 9, 2, 9] List after element removal: [1, 8, 4, 2]

Time complexity: O(n)
Auxiliary Space: O(n)

Using Recursion Method

Here we use recursive method which returns the NewList where the does not contains the given element. we recursively call the function for n number of times to traverse all value in a list.

Python3

def remove_element(begin,oldlist,value,newlist):

`` if begin = = len (oldlist):

`` return newlist

`` if value ! = oldlist[begin]:

`` newlist.append(oldlist[begin])

`` return remove_element(begin + 1 ,oldlist,value,newlist)

lst = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

value = 9

print ( "Original list:" , lst)

result = remove_element( 0 ,lst,value,[])

print ( "List after element removal:" , result)

Output

Original list: [1, 9, 8, 4, 9, 2, 9] List after element removal: [1, 8, 4, 2]

Time complexity: O(n)
Auxiliary Space: O(n)

Using enumeration

  1. Create a list list1 containing some elements.
  2. Print the initial list.
  3. Use a for loop and enumerate() function to iterate through the list and get the index and elements at that index.
  4. Check if the element at the current index is equal to the element to be removed.
  5. If it is equal, remove that element using pop() function and the current index.
  6. Print the updated list.

Python3

list1 = [ 1 , 9 , 8 , 4 , 9 , 2 , 9 ]

print ( "original list : " + str (list1))

for i, ele in enumerate (list1):

`` if ele = = 9 :

`` list1.pop(i)

print ( "List after element removal is : " + str (list1))

Output

original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]

The time complexity of the given code is O(n^2), where n is the length of the input list.

In the given code, we are iterating over each element of the input list using a for loop with enumerate(). Inside the loop, we are checking if the current element is equal to the value to be removed, and if it is, we are removing it using the pop() method. The pop() method has a time complexity of O(n), as it needs to shift all the subsequent elements by one index to fill the gap left by the removed element. Therefore, if we have multiple occurrences of the value to be removed, the time complexity can be much higher.

The auxiliary space of the given code is O(1), as we are not using any additional data structures to store the elements of the list. We are only modifying the original list in place.