I am trying to find a way of remove all the occurences of an item in a list in Python. To do so, imagine that my list is:
foo_list = [1,2,3,4,2,3]
And let's suppose I am trying to get rid of the item 2
. If I use the .remove
method, it will just delete the first 2
in my list.
foo_list.remove(2)
Will have as output [1,3,4,2,3]
but I would like to have as output [1,3,4,3]
. Of course I can do so using a comprehension list such as:
[item for item in foo_list if item !=2]
I could also do set(foo_list) but I do want to keep the replicates elements that are not the selected one, 2
in this case.
But I am trying to search for a way to do it without the need of a for loop as my real list has more than 100000 items, which it's making this procedure really slow. Is there any method similar to remove
that would allow me to delete all the selected items?
Any help would be appreciated.