-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
  • 11,699
  • 5
  • 39
  • 45
Help Pleasee
  • 181
  • 2
  • 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
  • 33,530
  • 5
  • 49
  • 82