Is is possible to break in lambda when the expected result is found
<p>I am Python newbie, and just become very interested in Lambda expression. The problem I have is to find one and only one target element from a list of elements with lambda filter. In theory, when the target element is found there is no sense to continue anymore. </p>

<p>With <code>for loop</code> it is pretty simple to <code>break</code> the loop, but what about by using <code>lambda</code>? is it after all possible to do this? I search from Google, but did not find the expected solution  </p>

<p>From <a href="https://docs.python.org/3/library/functions.html#filter" rel="nofollow">https://docs.python.org/3/library/functions.html#filter</a></p>

<blockquote>
  <p>Note that <code>filter(function, iterable)</code> is equivalent to the <strong>generator</strong>
  expression <code>(item for item in iterable if function(item))</code> if function
  is not <code>None</code> and <code>(item for item in iterable if item)</code> if function is
  <code>None</code>.</p>
</blockquote>

<p>So in practice your lambda will not be applied to whole list unless you start getting elements from it. So if you Only request one item from this generator you will get your functionality.</p>

<p>@edit:</p>

<p>To request one object you'd call <code>next(gen)</code></p>

<pre><code>lst = [1,2,3,4,5]
print(next(filter(lambda x: x%3==0, lst)))
</code></pre>

<p>Would output <code>3</code> and not process anything past <code>3</code> in <code>lst</code></p>

python loops lambda