[Python-ideas] Reduce/fold and scan with generator expressions and comprehensions

Steven D'Aprano steve at pearwood.info
Sun Oct 23 19:38:49 EDT 2016


On Mon, Oct 24, 2016 at 10:22:32AM +1100, Steven D'Aprano wrote:
> On Sun, Oct 23, 2016 at 08:47:12AM -0700, David Mertz wrote:
> > Consuming the iterator is *necessary* to get the last item. There's no way
> > around that.
> > 
> > Obviously, you could itertools.tee() it first if you don't mind the cache
> > space. But there cannot be a generic "jump to the end" of an iterator
> > without being destructive.
> 
> Right. But you're missing the point of Danilo's proposal. 

Ah, actually it may be that I have misunderstood Danilo's proposal, 
because his example does include BOTH a suggestion of new magic syntax 
for retrieving the *previous* loop value inside a comprehension AND what 
seems to be a new built-in(?) function last() which seems to do exactly 
what you suggest: jump right to the end of an iterable and return the 
final value.

My apologies for the confusion. 


-- 
Steve


More information about the Python-ideas mailing list