Python List remove() Method (original) (raw)
Last Updated : 01 May, 2025
Python list remove() function removes the first occurrence of a given item from list. It make changes to the current list. It only takes one argument, element we want to remove and if that element is not present in the list, it gives **ValueError.
**Example:
Python `
a = ['a', 'b', 'c'] a.remove("b") print(a)
`
**Explanation:
- remove() removes the first occurrence of the specified element from the list.
- In the example, “b” is removed from the list [‘a’, ‘b’, ‘c’], resulting in [‘a’, ‘c’].
Syntax of remove() method
list_name.remove(obj)
**Parameter:
- **obj: object to be removed from the list
**Return Type: 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.
Examples of remove() method
1. Passing list as argument in remove()
If we want to remove multiple elements from a list, we can do this by iterating through the second list and using the remove() method for each of its elements.
Python `
a = [1, 2, 3, 4, 5, 6, 7, 8] b = [2, 4, 6]
for item in b: if item in a: a.remove(item)
print(a)
`
**Explanation:
- The code loops through each element in list b and removes the corresponding elements from list a.
- remove() only removes the first occurrence of the specified element from the list.
- If an element from b doesn’t exist in a, it is skipped.
2. Trying to delete Element that doesn’t Exist
In this example, we are removing the element ‘e’ which does not exist in the list which will result in a ValueError. We can use a try except block to handle these exceptions.
Python `
a = [ 'a', 'b', 'c', 'd' ]
a.remove('e') print(a)
`
**Output:
Traceback (most recent call last):
File “Solution.py”, line 4, in
a.remove(‘e’)
ValueError: list.remove(x): x not in list
**Explanation:
- remove() method raises a ValueError if the specified element is not found in the list.
- The error message is: “list.remove(x): x not in list”.
**Related Articles:
Similar Reads
- Python List methods Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, we’ll explore all Python list methods with a simple example. List MethodsLet's look at different list methods in Python: append(): Adds a 3 min read
- Python List append() Method append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this. [GFGTABS] Python a = [2, 5, 6, 7] # Use append() to add the element 8 to the end of the list a.append( 3 min read
- Python List extend() Method In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once. Let’s look at a simple 2 min read
- Python List index() - Find Index of Item index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example: [GFGTABS] Python a = ["cat 3 min read
- Python List max() Method max() function in Python is a built-in function that finds and returns the largest element from the list. Let's understand it better with an example: [GFGTABS] Python a = [2,3,6,1,8,4,9,0] print(max(a)) [/GFGTABS]Output9 Syntaxmax(listname) Parameter: listname : Name of list in which we have to find 4 min read
- Python min() Function Python min() function returns the smallest value from a set of values or the smallest item in an iterable passed as its parameter. It's useful when you need to quickly determine the minimum value from a group of numbers or objects. For example: [GFGTABS] Python a = [23,25,65,21,98] print(min(a)) b = 4 min read
- How To Find the Length of a List in Python The length of a list refers to the number of elements in the list. There are several methods to determine the length of a list in Python. For example, consider a list l = [1, 2, 3, 4, 5], length of this list is 5 as it contains 5 elements in it. Let's explore different methods to find the length of 2 min read
- Python List pop() Method The pop() method is used to remove an element from a list at a specified index and return that element. If no index is provided, it will remove and return the last element by default. This method is particularly useful when we need to manipulate a list dynamically, as it directly modifies the origin 2 min read
- Python List remove() Method Python list remove() function removes the first occurrence of a given item from list. It make changes to the current list. It only takes one argument, element we want to remove and if that element is not present in the list, it gives ValueError. Example: [GFGTABS] Python a = ['a', 'b 3 min read
- Python List Reverse() The reverse() method is an inbuilt method in Python that reverses the order of elements in a list. This method modifies the original list and does not return a new list, which makes it an efficient way to perform the reversal without unnecessary memory uses. Let's see an example to reverse a list us 2 min read