Answer:
Code is given below along with explanation and output.
Step-by-step explanation:
we can use the method of list comprehension to achieve the given task.
In a list comprehension, we create a sub-list of elements which satisfy specific conditions. the condition i !=x makes sure that x would be excluded in the modified list.
def remove_all(L, x):
return [i for i in L if i != x]
L = [1, 2, 3, 4, 5, 6, 4, 3, 2, 4 ]
print('Before =',L)
L = remove_all(L, 4)
print('After =',L)
Output:
Before = [1, 2, 3, 4, 5, 6, 4, 3, 2, 4]
After = [1, 2, 3, 5, 6, 3, 2]
As you can see we have successfully removed the multiple occurrence of x=4 while preserving the order of the list.