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

Steven D'Aprano steve at pearwood.info
Sun Oct 23 11:42:41 EDT 2016


On Sun, Oct 23, 2016 at 08:37:07AM -0700, David Mertz wrote:
> Of course. But if you want last(), why not just spell the utility function
> as I did? I.e. as a function:
> 
>     def last(it):
>          for item in it:
>              pass
>         return item
> 
> That works fine for any iteratable (including a list, array, etc), whether
> or not it's a reduction/accumulation.

That's no good, because it consumes the iterator. Yes, you get 
the last value, but you actually needed to do work on all the 
previous values too.


-- 
Steve


More information about the Python-ideas mailing list