Python remove() is an inbuilt function in the Python programming language that removes a given object from the List.
Python List remove() Syntax
Syntax: list_name.remove(obj)
Parameters:
- obj: object to be removed from the list
Returns: The method does not return any value but removes the given object from the list.
Exception: If the element doesn’t exist, it throws ValueError: list.remove(x): x not in list exception.
Note: It removes the first occurrence of the object from the list.
Example of remove() in Python
Simple example to remove element from the list.
Python3
lis = ['a', 'b', 'c']
lis.remove("b")
print(lis)
|
Output:
['a', 'c']
In Python, the remove() method is a built-in list method used to remove the first occurrence of a specified element from a list. It modifies the list in place, removing the element if it exists, and raises a ValueError if the element is not found in the list
- Remove an element from the list
- Deleting Element that doesn’t Exist
- Remove Duplicates from List in Python
- Remove all Occurrences of a value from a List
- Removing a nested list element from a list
- Removing elements from a list based on a condition
- Removing elements from a list using the Filter function
Remove an element from the list
In this example, we are showing how we can use the remove() function with the Python list. Remove function removes the specified element’s first occurrence in the list.
Python3
list1 = [ 1, 2, 1, 1, 4, 5 ]
list1.remove(1)
print(list1)
list2 = [ 'a', 'b', 'c', 'd' ]
list2.remove('a')
print(list2)
|
Output
[2, 1, 1, 4, 5]
['b', 'c', 'd']
Time complexity: for the first list removal: O(n)
Time complexity: for the second list removal: O(1)
Space complexity: O(1) for both cases.
Deleting Element that doesn’t Exist
In this example, we are removing the element ‘e’ which does not exist.
Python3
list2 = [ 'a', 'b', 'c', 'd' ]
list2.remove('e')
print(list2)
|
Output
Traceback (most recent call last):
File "/home/e35b642d8d5c06d24e9b31c7e7b9a7fa.py", line 8, in
list2.remove('e')
ValueError: list.remove(x): x not in list
Remove Duplicates from List in Python
In this example, we are removing the element which comes multiple times in the list.
Python3
list2 = [ 'a', 'b', 'c', 'd', 'd', 'e', 'd' ]
list2.remove('d')
print(list2)
|
Output
['a', 'b', 'c', 'd', 'e', 'd']
Time complexity: O(n)
Space complexity: O(1)
Note: If a list contains duplicate elements, it removes the first occurrence of the object from the list.
Remove all Occurrences of a value from a List
Example 1: Remove all the 1’s from the list and print the list.
In this example, we remove 1 from the list until all 1 is removed.
Python3
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]
while (list1.count(1)):
list1.remove(1)
print(list1)
|
Output
[2, 3, 4, 4, 5]
Time complexity: O(n^2) – In the worst case, each call to list1.count(1) takes O(n) time because it needs to count the occurrences of ‘1’ in the list, and the loop runs n times.
Space complexity: O(1) – The space complexity is O(1) since we are only modifying the existing list and not creating any additional data structures proportional to the input size.
Example 2: Given a list, remove all the 2’s from the list using in keyword
In this example, we remove 2 from the list until all the 2 are removed.
Python3
mylist = [1, 2, 3, 2, 2]
while 2 in mylist:
mylist.remove(2)
print(mylist)
|
Output
[1, 3]
Time complexity: O(n^2)
Space complexity: O(1) – The space complexity is O(1) since we are only modifying the existing list and not creating any additional data structures proportional to the input size.
Removing a nested list element from a list
In this example, we are removing a list from a 2d List.
Python3
data = [[1, 2], [3, 4], [5, 6]]
data.remove([3, 4])
print(data)
|
Output
[[1, 2], [5, 6]]
Time complexity: O(n)
Space complexity: O(1)
Removing elements from a list based on a condition using a list comprehension
In this example, we are moving numbers from the list using list comprehension.
Python3
numbers = [1, 2, 3, 4, 5]
numbers = [x for x in numbers if x != 3]
print(numbers)
|
Output
[1, 2, 4, 5]
Time complexity: O(n)
Space complexity: O(1)
Removing elements from a list using the Filter function
In this example, we are using the lambda function to check the condition and filter out the data from the list.
Python3
numbers = [1, 2, 3, 4, 5]
numbers = list(filter(lambda x: x != 3, numbers))
print(numbers)
|
Output
[1, 2, 4, 5]
Time complexity: O(n)
Space complexity: O(n)