title: Python: Removal from a list of lambda closures
body: <p>I have confused myself up to the point I need help:</p>
  
  <p>Suppose I have a list of closures to be executed. Upon execution of such a closure, if a certain condition is met I want it to remove itself from the list. Consider the following:</p>
  
  <pre><code>my_lambdas = []
  
  def some_condition_is_met(n): return n &gt; 5
  
  def my_function(n):
      if some_condition_is_met(n):
          # Remove me from my_lambdas, but how?
          return False
      print(n)
      return True
  
  def make_me_a_lambda(n):
      # Alternatively do removal based on the return value:
      return lambda: my_function(n) or remove_me_but_how() 
  
  my_lambdas.append(make_me_a_lambda(1))
  my_lambdas.append(make_me_a_lambda(7))
  
  for l in my_lambdas: l()
  </code></pre>
  
  <p>In reality I don't have control over the execution the lambdas myself, so I can't say something like:</p>
  
  <pre><code>my_lambdas = [l for l in my_lambdas if l()]
  </code></pre>
  
  <p>Although I guess it would be possible to wrap the execution of the lambdas in a function I do have control over and pass only that single wrapper function to the host application, if possible I'd like to avoid that additional indirection.</p>
  
  <p>Any help is much appreciated, thanks!</p>