2

I am searching for items that are not repeated in a list in python. The current way I do it is,

python -mtimeit -s'l=[1,2,3,4,5,6,7,8,9]*99' '[x for x in l if l.count(x) == 1]'
100 loops, best of 3: 12.9 msec per loop

Is it possible to do it faster?

This is the output.

>>> l = [1,2,3,4,5,6,7,8,9]*99+[10,11]
>>> [x for x in l if l.count(x) == 1]
[10, 11]
Omair .
  • 324
  • 1
  • 12

2 Answers2

3

You can use the Counter class from collections:

from collections import Counter
...
[item for item, count in Counter(l).items() if count == 1]

My results:

$ python -m timeit -s 'from collections import Counter; l = [1, 2, 3, 4, 5, 6, 7, 8, 9] * 99' '[item for item, count in Counter(l).items() if count == 1]'
1000 loops, best of 3: 366 usec per loop
$ python -mtimeit -s'l=[1,2,3,4,5,6,7,8,9]*99' '[x for x in l if l.count(x) == 1]'
10 loops, best of 3: 23.4 msec per loop
Ry-
  • 209,133
  • 54
  • 439
  • 449
0

Basically you want to remove duplicate entries, so there are some answers here:

Using in as opposed to count() should be a little quicker because the query is done once it finds the first instance.

Community
  • 1
  • 1
eacousineau
  • 3,327
  • 2
  • 32
  • 37