Write a Python program to remove all the occurrences of an element from a list


The program is finding all occurrences of the value 1 in the list "val" and then removing those elements from the list. The variable "c" is used to store the number of occurrences of the value 1 in the list. The for loop then iterates that many times and removes an occurrence of the value 1 from the list each time through the loop using the remove() method. At the end of the loop, the modified list with all occurrences of 1 removed is printed.

Source Code

val = [1, 3, 4, 6, 5, 1]
a = 1
print ("Original list :" ,val)
c = val.count(a)
for i in range(c):
    val.remove(a)
print ("Remove operation :" , val)

Output

Original list : [1, 3, 4, 6, 5, 1]
Remove operation : [3, 4, 6, 5]


Example Programs