<div dir="ltr">It isn't uncommon to try and get either the first or the last True value from a list. In Python 2, you'd do this:<div></div><div class="markdown-here-wrapper" id="markdown-here-wrapper-295402" style>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">next((x <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">for</span> x <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">in</span> mylist <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">if</span> x))
</code></pre>
<p style="margin:1.2em 0px!important">And, in Python 3, thanks to filter returning an iterator, you’d do this:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">next(filter(bool,mylist))
</code></pre>
<p style="margin:1.2em 0px!important">It still is pretty common. Common enough to make it aggravating to write a function to do that nearly every time. My idea is to add <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">first</code> and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">last</code> functions that do just that.</p>
<p style="margin:1.2em 0px!important">Stuff that’s open to lots of debate:</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px"><p style="margin:1.2em 0px!important;margin:0.5em 0px!important">Names. They’re not very creative; I know.</p>
</li>
<li style="margin:0.5em 0px"><p style="margin:1.2em 0px!important;margin:0.5em 0px!important">Builtin or itertools. I’m personally leaning towards the latter at the moment.</p>
</li>
</ul>
<p style="margin:1.2em 0px!important">Implementing it in itertools would be simple:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial"><span class="function"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" style="color:rgb(153,0,0);font-weight:bold">first</span><span class="params">(it)</span>:</span>
<span class="keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> next(filter(bool,it))
<span class="function"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" style="color:rgb(153,0,0);font-weight:bold">last</span><span class="params">(it)</span>:</span>
<span class="keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> next(reversed(filter(bool,it)))</code></pre></div><div>-- <br><div dir="ltr">Ryan<div><div>If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."</div>
</div><div><br></div></div>
</div></div>