Remove Element by Value

Remove the first occurrence of a value from a list, if present.

PythonBeginner
Python
# Program to remove first occurrence of a value from list

items = input("Enter list elements separated by space: ").split()
value = input("Enter value to remove: ")

if value in items:
    items.remove(value)
    print("Updated list:", items)
else:
    print("Value not found in list.")

Output

Enter list elements separated by space: 1 2 3 2
Enter value to remove: 2
Updated list: ['1', '3', '2']

We use the list method .remove() which deletes the first matching element.