Is there a simple way to delete a list element by value?
Yes, you can use the remove()
method to delete a list element by its value. Here is an example:
This will remove the first occurrence of the value 3
in the list and print [1, 2, 4, 5]
.
Note that if the value is not in the list, this will raise a ValueError
. You can use in
keyword to check the existance of element in the list before calling remove method.