Is there a better/simpler way to filter blank lines?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Nov 5 04:07:20 CET 2008


On Tue, 04 Nov 2008 20:25:09 -0500, Steve Holden wrote:

> I think there'd be no advantage to a sort method on a generator, since
> theoretically the last item could be the first required in the sorted
> sequence, so it's necessary to hold all items in memory to ensure the
> sort is correct. So there's no point using a generator in the first
> place.


You can't sort something lazily.

Actually, that's not *quite* true: it only holds for comparison sorts. 
You can sort lazily using non-comparison sorts, such as Counting Sort:

http://en.wikipedia.org/wiki/Counting_sort

Arguably, the benefit of giving generators a sort() method would be to 
avoid an explicit call to list. But I think many people would argue that 
was actually a disadvantage, not a benefit, and that the call to list is 
a good thing. I'd agree with them.

However, sorted() should take a generator argument, and in fact I see it 
does:

>>> sorted( x+1 for x in (4, 2, 0, 3, 1) )
[1, 2, 3, 4, 5]



-- 
Steven



More information about the Python-list mailing list