sorted unique elements from a list; using 2.3 features

Andrew Dalke adalke at mindspring.com
Tue Jan 7 04:21:54 CET 2003


Steve Holden wrote:
> Presumably (I don't have 2.3 to hand, so I may get shot down in flames)
> since a set has __iter__() one could also use
> 
>     subset = [x for x in sets.Set(data)]
> 
> to create the list you want to sort? I'm not convinced this is a win,
> though, so timings might be interesting.

Yes.  That's actually what I started off doing until I remembered
that 'list' takes an iterator in the constructor.  (All them new-fangled
things these days!)


> When sorting isn't required, of course, it should be perfectly possible to
> simply write:
> 
>     for element in sets.Set(data):
>         do_stuff_with(element)
> 
> and no list conversion is necessary since the iterator does what it's
> supposed to (i.e.: support iteration :-)

And for that specific case

   map(do_stuff_with, sets.Set(data))

That gets to a level of terseness which makes my head hurt a bit. :)


					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list