-5

Assuming 'a' is a list that contains (for example) [2,4,2,3,3,3]

for i in a:
    if a.count(i) == i:
        a.remove(i)

How can I convert the code to one line using list comprehenstion, lambda, filter or something diffrent.

Matthias
  • 12,873
  • 6
  • 42
  • 48
Help Pleasee
  • 211
  • 3
  • 15

1 Answers1

2

Never remove items from a list while iterating over it, the iteration gets messed up and you won't get the desired result.

>>> [x for i, x in enumerate(a) if a.count(x) != x or a.index(x) != i]
[4, 2, 3, 3]
Alex Hall
  • 34,833
  • 5
  • 57
  • 89