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:
my_list = [1, 2, 3, 4, 5] my_list.remove(3) print(my_list)
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.
if 3 in my_list: my_list.remove(3)