[Tutor] Yet another list comprehension question

Andrei project5 at redrival.net
Fri Mar 2 18:18:36 CET 2007


> "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])
> 

Alternatively, you could put the results as keys in a dictionary, then request
mydict.keys() to get a list of unique outcomes.

Yours,

Andrei



More information about the Tutor mailing list