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

Steven D'Aprano steve at pearwood.info
Sun Oct 16 18:55:41 EDT 2016


On Sun, Oct 16, 2016 at 02:34:58PM +0200, Sven R. Kunze wrote:

> As this discussion won't come to an end, I decided to consult my girlfriend.
[...]
> >>> [(i,i,i) for i in range(4)]
> [(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]

Did you remember to tell your girlfriend that a critical property of 
the "??? for i in range(4)" construct is that it generates one value per 
loop? It loops four times, so it generates exactly four values (in this 
case, each value is a bracketed term).


> Let's remove these inner parentheses again.
> 
> >>> [*(i,i,i) for i in range(4)]
>   File "<stdin>", line 1
> SyntaxError: iterable unpacking cannot be used in comprehension

It loops four times, so it must generate four values. What's the star 
supposed to do? Turn four loops into twelve?



-- 
Steve


More information about the Python-ideas mailing list