Alan Gauld wrote:
> "Smith, Jeff" <[EMAIL PROTECTED]> 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:
http://www.effbot.org/pyfaq/how-do-you-remove-duplicates-from-a-list.htm

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.

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to