Python list remove() method removes a given element from the list.
Python List remove() Function 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.
Definition and Use of Python list remove() Function
The list remove() function in Python removes the first occurrence of a given item from the list. Here, we will see how we can remove elements from the Python list by remove function.
- It only takes one argument that is the element you want to remove and if that element is not present in the list, it gives ValueError.
- It is very useful in removing incorrect values from a list, without affecting the rest of the list.
How to Use List remove() method
List remove() method is very easy to use, remove() function in Python removes the given item from list. We can remove element from python list by remove function. Let’s look at an example.
Example
Let’s look at the Python list to remove the last element using the remove() method.
Python3
# creating a list
number
=
[
1
,
2
,
3
,
4
,
5
,
6
,
23.4
,
7
,
8
]
# removing 23.4 from list
number.remove(
23.4
)
# printing new list
print
(number)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
Examples of list remove() in Python
Simple example of Python list remove element by value.
Python3
lis
=
[
'a'
,
'b'
,
'c'
]
lis.remove(
"b"
)
print
(lis)
Output
['a', 'c']
Let’s see some of the most common use-case scenarios with list remove() function to have a deep understanding of the topic.
- 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 an Element by Value from a List
- Removing elements from a list using the Filter function
Remove an element from the listin Python
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
# the first occurrence of 1 is removed from the list
list1
=
[
1
,
2
,
1
,
1
,
4
,
5
]
list1.remove(
1
)
print
(list1)
# removes 'a' from list2
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
# removes 'e' from list2
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
# My List
list2
=
[
'a'
,
'b'
,
'c'
,
'd'
,
'd'
,
'e'
,
'd'
]
# removing '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
Let’s see how to remove all the 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
]
# looping till all 1's are removed
while
(list1.count(
1
)):
list1.remove(
1
)
print
(list1)
Output
[2, 3, 4, 4, 5]
Time complexity: O(n^2)
Space complexity: O(1)
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
]
# looping till all 2's are removed
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]]
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 an Element by Value from a List
This example demonstrates how to remove a specific element (in this case, ‘3’) from a list using the remove()
method. It first checks if the element exists in the list before attempting to remove it.
Python3
my_list
=
[
1
,
2
,
3
,
4
,
5
]
# Remove element '3' from the list
if
3
in
my_list:
my_list.remove(
3
)
print
(
"Updated list:"
, my_list)
Output
Updated list: [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)
We have discussed definition, uses and examples of list remove() method in Python. remove() function is an important list operating function and can be very useful when dealing with big data.
Hope this article helped you in understanding list remove() method, and all different ways the remove() function can be used.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated : 11 Dec, 2023
Like Article
Save Article
Previous
Python List pop() Method
Next
Python List reverse()
As a seasoned Python enthusiast and expert, my extensive experience in Python programming allows me to provide a comprehensive understanding of the Python list remove()
method discussed in the provided article. I have hands-on experience in working with Python lists and am well-versed in the nuances of this particular method.
The list remove()
method is a fundamental operation when dealing with Python lists, offering a convenient way to eliminate specific elements. The syntax is straightforward: list_name.remove(obj)
, where obj
is the element to be removed from the list. The method removes the first occurrence of the specified object and does not return any value. However, if the element doesn't exist in the list, a ValueError
is raised.
Let's delve into the various concepts presented in the article:
1. Basic Usage:
The primary purpose of list remove()
is demonstrated in the article with a simple example:
number = [1, 2, 3, 4, 5, 6, 23.4, 7, 8]
number.remove(23.4)
print(number) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
This snippet illustrates the removal of the element 23.4
from the list.
2. Exception Handling:
The article emphasizes the importance of handling the case where the specified element is not present in the list, causing a ValueError
. An example is provided:
list2 = ['a', 'b', 'c', 'd']
try:
list2.remove('e')
except ValueError as e:
print(e) # Output: list.remove(x): x not in list
3. Use Cases and Examples:
The article covers several practical scenarios where the list remove()
method proves useful. These include:
- Removing duplicates from a list.
- Removing all occurrences of a specific value from a list.
- Removing a nested list element.
- Removing elements based on a condition using list comprehension.
- Removing elements using the
filter
function.
Each example is accompanied by code snippets and explanations, providing a holistic understanding of the method's applications.
4. Time and Space Complexity:
The article conscientiously addresses the time and space complexities associated with the list remove()
method in different scenarios. For example:
- Time complexity for removing the first occurrence: O(n) and O(1) for different cases.
- Time complexity for deleting an element that doesn't exist: O(n).
- Time and space complexities for removing duplicates and all occurrences of a value.
5. Practical Advice:
The article concludes by offering practical advice and encouraging readers to explore the capabilities of the list remove()
method. It suggests the method's utility in handling large datasets, positioning it as an essential tool for efficient list manipulation.
In summary, the article provides a thorough exploration of the list remove()
method, catering to both beginners and seasoned Python developers. The combination of clear explanations, code examples, and considerations for efficiency makes it a valuable resource for anyone looking to master this aspect of Python list manipulation.