+2 votes
in Programming Languages by (51.8k points)
How can I delete all occurrences of an element from a Python list?

For example, I want to delete 10 from the following list:

[10,11,12,10,23,34,10,56]

1 Answer

+2 votes
by (281k points)
selected by
 
Best answer

You can try list comprehension to delete all occurrences of an element from a Python list.

Here is an example:

>>> aa=[10,11,12,10,23,34,10,56]

>>> a=[v for v in aa if v!= 10]

>>> a

[11, 12, 23, 34, 56]


...