<p dir="ltr">What is the intuition behind [1, *x, 5]? The starred expression is replaced with a comma-separated sequence of its elements.</p>
<p dir="ltr">The trailing comma Nick referred to is there, with the rule that [1,, 5] is the same as [1, 5].</p>
<p dir="ltr">All the examples follow this intuition, IIUC. </p>
<p dir="ltr">Elazar</p>
<br><div class="gmail_quote"><div dir="ltr">בתאריך יום ד׳, 12 באוק' 2016, 22:22, מאת David Mertz ‏<<a href="mailto:mertz@gnosis.cx">mertz@gnosis.cx</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr" class="gmail_msg">I've followed this discussion some, and every example given so far completely mystifies me and I have no intuition about what they should mean.</p>
<div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">On Oct 12, 2016 8:43 AM, "Steven D'Aprano" <<a href="mailto:steve@pearwood.info" class="gmail_msg" target="_blank">steve@pearwood.info</a>> wrote:<br type="attribution" class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Tue, Oct 11, 2016 at 02:42:54PM +0200, Martti Kühne wrote:<br class="gmail_msg">
> Hello list<br class="gmail_msg">
><br class="gmail_msg">
> I love the "new" unpacking generalisations as of pep448. And I found<br class="gmail_msg">
> myself using them rather regularly, both with lists and dict.<br class="gmail_msg">
> Today I somehow expected that [*foo for foo in bar] was equivalent to<br class="gmail_msg">
> itertools.chain(*[foo for foo in bar]), which it turned out to be a<br class="gmail_msg">
> SyntaxError.<br class="gmail_msg">
<br class="gmail_msg">
To me, that's a very strange thing to expect. Why would you expect that<br class="gmail_msg">
unpacking items in a list comprehension would magically lead to extra<br class="gmail_msg">
items in the resulting list? I don't think that makes any sense.<br class="gmail_msg">
<br class="gmail_msg">
Obviously we could program list comprehensions to act that way if we<br class="gmail_msg">
wanted to, but that would not be consistent with the ordinary use of<br class="gmail_msg">
list comprehensions. It would introduce a special case of magical<br class="gmail_msg">
behaviour that people will have to memorise, because it doesn't follow<br class="gmail_msg">
logically from the standard list comprehension design.<br class="gmail_msg">
<br class="gmail_msg">
The fundamental design principle of list comps is that they are<br class="gmail_msg">
equivalent to a for-loop with a single append per loop:<br class="gmail_msg">
<br class="gmail_msg">
    [expr for t in iterable]<br class="gmail_msg">
<br class="gmail_msg">
is equivalent to:<br class="gmail_msg">
<br class="gmail_msg">
    result = []<br class="gmail_msg">
    for t in iterable:<br class="gmail_msg">
        result.append(expr)<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
If I had seen a list comprehension with an unpacked loop variable:<br class="gmail_msg">
<br class="gmail_msg">
    [*t for t in [(1, 'a'), (2, 'b'), (3, 'c')]]<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
I never in a million years would expect that running a list<br class="gmail_msg">
comprehension over a three-item sequence would magically expand to six<br class="gmail_msg">
items:<br class="gmail_msg">
<br class="gmail_msg">
    [1, 'a', 2, 'b', 3, 'c']<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
I would expect that using the unpacking operator would give some sort<br class="gmail_msg">
of error, or *at best*, be a no-op and the result would be:<br class="gmail_msg">
<br class="gmail_msg">
    [(1, 'a'), (2, 'b'), (3, 'c')]<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
append() doesn't take multiple arguments, hence a error should be the<br class="gmail_msg">
most obvious result. But if not an error, imagine the tuple unpacked to<br class="gmail_msg">
two arguments 1 and 'a' (on the first iteration), then automatically<br class="gmail_msg">
packed back into a tuple (1, 'a') just as you started with.<br class="gmail_msg">
<br class="gmail_msg">
I think it is a clear, obvious and, most importantly, desirable property<br class="gmail_msg">
of list comprehensions with a single loop that they cannot be longer<br class="gmail_msg">
than the initial iterable that feeds them. They might be shorter, if you<br class="gmail_msg">
use the form<br class="gmail_msg">
<br class="gmail_msg">
    [expr for t in iterable if condition]<br class="gmail_msg">
<br class="gmail_msg">
but they cannot be longer.<br class="gmail_msg">
<br class="gmail_msg">
So I'm afraid I cannot understand what reasoning lead you to<br class="gmail_msg">
expect that unpacking would apply this way. Wishful thinking<br class="gmail_msg">
perhaps?<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
Steve<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
Python-ideas mailing list<br class="gmail_msg">
<a href="mailto:Python-ideas@python.org" class="gmail_msg" target="_blank">Python-ideas@python.org</a><br class="gmail_msg">
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" class="gmail_msg" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br class="gmail_msg">
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" class="gmail_msg" target="_blank">http://python.org/psf/codeofconduct/</a><br class="gmail_msg">
</blockquote></div></div>
_______________________________________________<br class="gmail_msg">
Python-ideas mailing list<br class="gmail_msg">
<a href="mailto:Python-ideas@python.org" class="gmail_msg" target="_blank">Python-ideas@python.org</a><br class="gmail_msg">
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" class="gmail_msg" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br class="gmail_msg">
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" class="gmail_msg" target="_blank">http://python.org/psf/codeofconduct/</a></blockquote></div>