[Python-ideas] Fwd: Fwd: Fwd: unpacking generalisations for list comprehension
Greg Ewing
greg.ewing at canterbury.ac.nz
Sat Oct 15 19:48:36 EDT 2016
Steven D'Aprano wrote:
> Are you now supporting my argument that starring the list comprehension
> expression isn't meaningful?
The context it's in (a form of list display) has a clear
meaning for a comma-separated list of values, so there
is a reasonable interpretation that it *could* be given.
> py> iterable = [(1, 'a'), (2, 'b')]
> py> [(100, *t) for t in iterable]
> [(100, 1, 'a'), (100, 2, 'b')]
The * there is in the context of constructing a tuple,
not the list into which the tuple is placed.
The difference is the same as the difference between
these:
>>> t = (10, 20)
>>> [1, (2, *t), 3]
[1, (2, 10, 20), 3]
>>> [1, 2, *t, 3]
[1, 2, 10, 20, 3]
--
Greg
More information about the Python-ideas
mailing list