<div dir="ltr">IIUC, this would be similar to "first" ( <a href="https://pypi.python.org/pypi/first/">https://pypi.python.org/pypi/first/</a> ) but would raise exception in case the iterable returns more than one (or less than one) element.<div><br></div><div>Would also be similar to one() in SQLAlchemy queries ( <a href="http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.one">http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.one</a> ).</div><div><br></div><div>Regards,<br><div><br></div><div> S.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 30, 2017 at 5:51 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Mon, Oct 30, 2017 at 07:14:10AM +0300, Ivan Pozdeev via Python-ideas wrote:<br>
<br>
> The eponymous C#'s LINQ method, I found very useful in the following,<br>
> quite recurring use-case:<br>
<br>
</span>If I have understood your use-case, you have a function that returns a<br>
list of results (or possibly an iterator, or a tuple, or some other<br>
sequence):<br>
<br>
print(search(haystack, needle))<br>
# prints ['bronze needle', 'gold needle', 'silver needle']<br>
<br>
There are times you expect there to be a single result, and if there are<br>
multiple results, that is considered an error. Am I correct so far?<br>
<br>
If so, then sequence unpacking is your friend:<br>
<br>
result, = search(haystack, needle)<br>
<br>
Note the comma after the variable name on the left-hand side of the<br>
assignment. That's a special case of Python's more general sequence<br>
unpacking:<br>
<br>
a, b, c = [100, 200, 300]<br>
<br>
assigns a = 100, b = 200, c == 300. Using a single item is valid:<br>
<br>
py> result, = [100]<br>
py> print(result)<br>
100<br>
<br>
<br>
but if the right-hand side has more than one item, you get an exception:<br>
<br>
py> result, = [100, 200, 300]<br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in <module><br>
ValueError: too many values to unpack (expected 1)<br>
<br>
<br>
I *think* this will solve your problem.<br>
<br>
If not, can you please explain what "single()" is supposed to do, why it<br>
belongs in itertools, and show an example of how it will work.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Steve<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Stefane Fermigier - <a href="http://fermigier.com/" target="_blank">http://fermigier.com/</a> - <a href="http://twitter.com/sfermigier" target="_blank">http://twitter.com/sfermigier</a> - <a href="http://linkedin.com/in/sfermigier" target="_blank">http://linkedin.com/in/sfermigier</a><br>Founder & CEO, Abilian - Enterprise Social Software - <a href="http://www.abilian.com/" target="_blank">http://www.abilian.com/</a><br>Chairman, Free&OSS Group / Systematic Cluster - <a href="http://www.gt-logiciel-libre.org/" target="_blank">http://www.gt-logiciel-libre.org/</a><br>Co-Chairman, National Council for Free & Open Source Software (CNLL) - <a href="http://cnll.fr/" target="_blank">http://cnll.fr/</a><div>Founder & Organiser, PyData Paris - <a href="http://pydata.fr/" target="_blank">http://pydata.fr/</a><br></div><div>---</div><div><div>“You never change things by fighting the existing reality. To change something, build a new model that makes the existing model obsolete.” <span style="font-size:12.8px">— R. Buckminster Fuller</span></div></div><div><br><br></div></div></div></div></div></div></div></div>
</div>