Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can call func() beforehand, and you want to combine the test and assignment statements into a single statement, then you can do this, with an if-else expression:</p> <pre><code>b += [a] if a is not None else [] </code></pre> <p>If a is not None, then this will add [a] to b -- essentially the same operation as b.append(a)</p> <p>If a <em>is</em> None, then this will add [] to b, which will leave b unchanged.</p> <p>This won't work unless b is a list, or at least supports "+=" in-place addition. If it doesn't -- perhaps it's some custom object, then you should be able to do this:</p> <pre><code>(b.append(a) if a is not None else None) </code></pre> <p>This is an expression, evaluated for its side effects, and then thrown away. If a is None, then the <code>b.append(a)</code> call will never be executed. In either case, the value of the expression is None, but we don't care about it, so it gets ignored.</p> <p>Now, if you want to combine the func() call with this, then you'll have to do something different in order to avoid calling func twice. If you can use the "+=" syntax, then you can do it like this:</p> <pre><code>b += filter(None, [func()]) </code></pre> <p><code>filter(None, &lt;list&gt;)</code> returns the list with all false elements (None included, but also 0 and []) removed. This statement, then, will add either [func()] or [] to b.</p> <p><strong>[Edited]</strong></p> <p>Finally, for the worst case scenario: If you can't call func() more than once, <em>and</em> you can't use <code>b += &lt;list&gt;</code>, <em>and</em> you need to accept 0, "", [], etc, and only exclude <code>None</code>, <em>and</em> you need it all on one line, here's the ugliest line of code yet:</p> <pre><code>(lambda l, a: l.append(a) if a is not None else None)(b, func()) </code></pre> <p>This is essentially @ekhumoro's solution, compressed into one line. It defines an anonymous function, calls it, discards the value, and then discards the function, all for the sake of the side effect.</p> <p>Now, this is a single line, but it's certainly not easier to read or understand than the original code. If I were you, I'd stick with the original, or go with @ekhumoro's idea of just defining a helper function and using that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload