![]() |
VOOZH | about |
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.
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.
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)
We can remove an item from the list by passing the value of the item to be deleted as the parameter to remove() function.
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)
In this method, we are using list comprehension. Here, we are appending all the elements except the elements that have to be removed.
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)
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.
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
Since the list is converted to set, all duplicates are removed, but the ordering of the list cannot be preserved.
Output:
original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [8, 1, 2, 4]
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.
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)
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.
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)
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.