[Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

Chris Angelico rosuav at gmail.com
Thu Apr 12 16:26:27 EDT 2018


On Wed, Apr 11, 2018 at 1:41 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Personally, I still think the best approach here is a combination of
> itertools.accumulate, and the proposed name-binding as an expression
> feature:
>
>     total = 0
>     running_totals = [(total := total + x) for x in values]
>     # alternative syntax
>     running_totals = [(total + x as total) for x in values]
>
> If you don't like the dependency on an external variable (or if that
> turns out not to be practical) then we could have:
>
>     running_totals = [(total := total + x) for total in [0] for x in values]
>

Linking this to the PEP 572 thread, this is an open question now:

https://www.python.org/dev/peps/pep-0572/#importing-names-into-comprehensions

Anyone who's interested in (or intrigued by) this potential syntax is
very much welcome to hop over to the PEP 572 threads and join in.

ChrisA


More information about the Python-ideas mailing list