title: Assignment inside lambda expression in Python
body: <p>I have a list of objects and I want to remove all objects that are empty except for one, using <code>filter</code> and a <code>lambda</code> expression.</p>
  
  <p>For example if the input is:</p>
  
  
  
  <pre><code>[Object(name=""), Object(name="fake_name"), Object(name="")]
  </code></pre>
  
  <p>...then the output should be:</p>
  
  
  
  <pre><code>[Object(name=""), Object(name="fake_name")]
  </code></pre>
  
  <p>Is there a way to add an assignment to a <code>lambda</code> expression? For example:</p>
  
  
  
  <pre><code>flag = True 
  input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
  output = filter(
      (lambda o: [flag or bool(o.name), flag = flag and bool(o.name)][0]),
      input
  )
  </code></pre>