[Tutor] Yet another list comprehension question

Kent Johnson kent37 at tds.net
Sat Mar 3 14:47:55 CET 2007

Alan Gauld wrote:
> "Smith, Jeff" <jsmith at medplus.com> wrote
>> In other words, applying somefun to the results of the iterator 
>> return
>> duplicates but I want the constructed list to contain none.
>> l = [somefun(i) for i some-iterator if somefun(i) not in l]
>> doesn't work (not that I expected it to).
> Why not use a Set?
> s = Set([somefun(i) for i in some-iterator])
> Might be slow for big lists though...

This is a popular question. It comes up frequently on comp.lang.python 
and there are many recipes in the online cookbook. Here is a FAQ:

I think using a set is the fastest solution if the list items are 
hashable and you don't care about order.

If the list items are hashable and you do care about order then there is 
this mild hack:
s = set()
[ i for i in lst if i not in s and not s.add(i) ]

but with the requirement of calling somefunc(i) my guess is that an 
explicit loop will be faster.

The cookbook is down right now but there is a link in the above FAQ.


More information about the Tutor mailing list