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".

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to