<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Feb 18, 2014, at 14:25, Ryan Gonzalez <<a href="mailto:rymg19@gmail.com">rymg19@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><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></div></div></div></blockquote><div>If suggest putting it on PyPI, or submitting it to an existing project like more-itertools, and seeing what kind of uptake it has, before trying to decide whether it's necessary to add to the stdlib.</div><div><br></div><div>Except I think first may already be in more-itertools.</div><blockquote type="cite"><div><div dir="ltr"><div class="markdown-here-wrapper" id="markdown-here-wrapper-295402" style=""><ul style="margin:1.2em 0px;padding-left:2em"><li style="margin:0.5em 0px">
</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></div></blockquote><div>But reversed takes a sequence, not an iterator. So you'd have to call it on list(filter(...)), and surely you don't want to build the whole list just to get the last value. You could build it by using a single-element peek wrapper, or just an explicit for loop that keeps track of "last" as it goes along.</div><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><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>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br><span>Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a></span></div></blockquote></body></html>