<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Putting it another way, your example doesn't make sense.  How
      would you parenthesise it to make it clearer?<br>
    </p>
    <p>    [ (x for x  in a if x & 1) else 'even']     You have an
      "else" without an "if".</p>
    <p>    [ x for x  in (a if x & 1 else 'even')]     Using x
      before it has been defined, at least in this line of code.</p>
    <p>    [ (x for x  in a) if x & 1 else 'even']     Ditto</p>
    <p>Other variants may be possible.  Whereas Jelle's correct version
      can be written as<br>
    </p>
    <p>    [(x if x & 1 else 'even') for x in a]        [1, 'even',
      3]</p>
    <p>Rob Cliffe<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 05/10/2017 16:44, Jelle Zijlstra
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAFp3-p_dmtmDZQfh6tRFmEG1jFopGUZORbTv_z1SJwJ5yNi2BQ@mail.gmail.com">
      <div dir="ltr">[x if x & 1 else 'even' for x in a]
        <div><br>
        </div>
        <div>An `if` at the end of the comprehension means a condition
          on whether to include the value.</div>
        <div><br>
        </div>
        <div>Also, this question would have been better asked on
          python-list.</div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">2017-10-05 8:40 GMT-07:00 Jason H <span
            dir="ltr"><<a href="mailto:jhihn@gmx.com" target="_blank"
              moz-do-not-send="true">jhihn@gmxcom</a>></span>:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">>>>
            a = [1,2,3]<br>
            >>> [ x for x  in a if x & 1]<br>
            [1, 3]<br>
            >>> [ x for x  in a if x & 1 else 'even']<br>
              File "<stdin>", line 1<br>
                [ x for x  in a if x & 1 else 'even']<br>
                                            ^<br>
            SyntaxError: invalid syntax<br>
            <br>
            I expected [1, 'even', 3]<br>
            <br>
            I would expect that the if expression would be able to
            provide alternative values through else.<br>
            <br>
            The work around blows it out to:<br>
            l = []<br>
            for x in a:<br>
              if x&1:<br>
                l.append(x)<br>
              else:<br>
                l.append('even')<br>
            <br>
            <br>
            Unless there is a better way?<br>
            ______________________________<wbr>_________________<br>
            Python-ideas mailing list<br>
            <a href="mailto:Python-ideas@python.org"
              moz-do-not-send="true">Python-ideas@python.org</a><br>
            <a
              href="https://mail.python.org/mailman/listinfo/python-ideas"
              rel="noreferrer" target="_blank" moz-do-not-send="true">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
            Code of Conduct: <a
              href="http://python.org/psf/codeofconduct/"
              rel="noreferrer" target="_blank" moz-do-not-send="true">http://python.org/psf/<wbr>codeofconduct/</a><br>
          </blockquote>
        </div>
        <br>
      </div>
      <div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br>
        <table style="border-top: 1px solid #D3D4DE;">
          <tbody>
            <tr>
              <td style="width: 55px; padding-top: 13px;"><a
href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient"
                  target="_blank" moz-do-not-send="true"><img
src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-green-avg-v1.png"
                    alt="" style="width: 46px; height: 29px;"
                    moz-do-not-send="true" height="29" width="46"></a></td>
              <td style="width: 470px; padding-top: 12px; color:
                #41424e; font-size: 13px; font-family: Arial, Helvetica,
                sans-serif; line-height: 18px;">Virus-free. <a
href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient"
                  target="_blank" style="color: #4453ea;"
                  moz-do-not-send="true">www.avg.com</a> </td>
            </tr>
          </tbody>
        </table>
        <a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1"
          height="1" moz-do-not-send="true"> </a></div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Python-ideas mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-ideas@python.org">Python-ideas@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a>
Code of Conduct: <a class="moz-txt-link-freetext" href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>