Python list Method - remove()
The remove() method in Python is used to remove the first occurrence of a specified element from a list. The method modifies the original list in place and does not return anything.
The syntax for the remove() method is as follows:
list.remove(element)
Here, list refers to the list from which the element needs to be removed, and element is the object to be removed.
Example:
# Creating a list my_list = [1, 2, 3, 2, 4] # Removing the element 2 my_list.remove(2) # Displaying the modified list print(my_list) # Output: [1, 3, 2, 4]
In the above example, the remove() method is used to remove the first occurrence of the integer 2 from the list my_list. The original list is modified and the output shows the updated list. If the element is not present in the list, the remove() method will raise a ValueError.
