[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension

David Mertz mertz at gnosis.cx
Thu Oct 13 14:50:17 EDT 2016


>
>     [*t for t in [(1, 'a'), (2, 'b'), (3, 'c')]]
>

Another problem with this is that it is very hard to generalize to the case
where the item included in a comprehension is a transformation on iterated
values.  E.g. what does this do?

    [math.exp(*t) for t in [(1,2),(3,4)]]

Maybe that somehow magically gets us:

    [2.7182, 7.38905, 20.0855, 54.5981]

Or maybe the syntax would be:

    [*math.exp(t) for t in [(1,2),(3,4)]]

Neither of those follows conventional Python semantics for function calling
or sequence unpacking.  So maybe that remains a type error or syntax
error.  But then we exclude a very common pattern of using comprehensions
to create collections of *transformed* data, not simply of filtered data.

In contrast, either of these are unambiguous and obvious:

    [math.exp(t) for t in flatten([(1,2),(3,4)])]

Or:

    [math.exp(n) for t in [(1,2),(3,4)] for n in t]

Obviously, picking math.exp() is arbitrary and any unary function would be
the same issue.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161013/e3c6cd4c/attachment.html>


More information about the Python-ideas mailing list