Remove Element by Value
Remove the first occurrence of a value from a list, if present.
BeginnerTopic: List Programs
Python Remove Element by Value Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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']
Understanding Remove Element by Value
We use the list method .remove() which deletes the first matching element.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.