[Python-ideas] Fwd: unpacking generalisations for list comprehension
Sven R. Kunze
srkunze at mail.de
Wed Oct 12 16:39:17 EDT 2016
On 12.10.2016 21:38, אלעזר wrote:
>
> What is the intuition behind [1, *x, 5]? The starred expression is
> replaced with a comma-separated sequence of its elements.
>
> The trailing comma Nick referred to is there, with the rule that [1,,
> 5] is the same as [1, 5].
>
I have to admit that I have my problems with this "comma-separated
sequence" idea. For me, lists are just collections of items. There are
no commas involved. I also think that thinking about commas here
complicates the matter.
What * does, it basically plugs in the items from the starred expression
into its surroundings:
[*[1,2,3]] = [1,2,3]
Let's plug in two lists into its surrounding list:
[*[1,2,3], *[1,2,3]] = [1,2,3,1,2,3]
So, as the thing goes, it looks like as if * could just work anywhere
inside those brackets:
[*[1,2,3] for _ in range(3)] = [*[1,2,3], *[1,2,3], *[1,2,3]] =
[1,2,3,1,2,3,1,2,3]
I have difficulties to understand the problem of understanding the
syntax. The * and ** variants just flow naturally whereas the "chain"
equivalent is bit "meh".
Cheers,
Sven
More information about the Python-ideas
mailing list