<div dir="ltr">You should give an actual motivating example.  I think none of these suggestions are more readable than just writing things out as a for loop.  You argue that you want to avoid appending to a result list.  In that case, I suggest writing your pattern as a generator function.<div><br></div><div>Best,</div><div><br></div><div>Neil<br><div><br>On Thursday, February 15, 2018 at 2:03:31 AM UTC-5, fhsxfhsx wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div>As far as I can see, a comprehension like <br><span style="font-family:monospace">alist = [f(x) for x in range(10)]<br></span>is better than a for-loop<br><span style="font-family:monospace">for x in range(10):<br>  alist.append(f(x))<br></span>because the previous one shows every element of the list explicitly so that we don't need to handle `append` mentally.<br><br>But when it comes to something like<br><span style="font-family:monospace">[f(x) + g(f(x)) for x in range(10)]<br></span>you find you have to sacrifice some readableness if you don't want two f(x) which might slow down your code.<br><br>Someone may argue that one can write<br><span style="font-family:monospace">[y + g(y) for y in [f(x) for x in range(10)]]<br></span>but it's not as clear as to show what `y` is in a subsequent clause, not to say there'll be another temporary list built in the process.<br>We can even replace every comprehension with map and filter, but that would face the same problems.<br><br>In a word, what I'm arguing is that we need a way to assign temporary variables in a comprehension.<br>In my opinion, code like<br><span style="font-family:monospace">[y + g(y) for x in range(10) **some syntax for `y=f(x)` here**]<br></span>is more natural than any solution we now have.<br>And that's why I pro the new syntax, it's clear, explicit and readable, and is nothing beyond the functionality of the present comprehensions so it's not complicated.<br><br>And I hope the discussion could focus more on whether we should allow assigning temporary variables in comprehensions rather than how to solve the specific example I mentioned above.<br><span style="font-family:monospace"></span><span style="font-family:monospace"></span><span style="font-family:monospace"></span><span style="font-family:monospace"></span></div></div><br><br><span title="neteasefooter"><p> </p></span></blockquote></div></div></div>