I would think that you can use tuple unpacking for this?
single_element, = some_iterator
Will attempt to unpack the some_iterator into single_element and
fail if single_element doesn't have exactly 1 element. It also has
the added benefit that it works for any number of elements:
one, two = some_iterator
Example:
>>> a = [1]
>>> one, = a
>>> one
1
>>> b = [1, 2]
>>> one, = b
---------------------------------------------------------------------------
ValueError Traceback (most
recent call last)
<ipython-input-5-9b70ee0300f5> in <module>
----> 1 one, = b
ValueError: too many values to unpack (expected 1)
>>> c = []
>>> one, = c
---------------------------------------------------------------------------
ValueError Traceback (most
recent call last)
<ipython-input-7-413c1b218942> in <module>
----> 1 one, = c
ValueError: not enough values to unpack (expected 1, got 0)
Best,
Paul
I second this as being useful.
However the “pythonic” way (whatever that means nowadays) is to do a for break else loop, which I think is kinda difficult to read as you need to make a few assumptions.
Rollo
On 27 Jul 2020, at 20:06, Noam Yorav-Raphael <noamraph@gmail.com> wrote:
_______________________________________________Hi,
There's a simple function that I use many times, and I think may be a good fit to be added to itertools. A function that gets an iterator, and if it has exactly one element returns it, and otherwise raises an exception. This is very useful for cases where I do some sort of query that I expect to get exactly one result, and I want an exception to be raised if I'm wrong. For example:
jack = one(p for p in people if p.id == '1234')
sqlalchemy already has such a function for queries: https://docs.sqlalchemy.org/en/13/orm/query.html#sqlalchemy.orm.query.Query.one
This is my implementation:
def one(iterable):
it = iter(iterable)
try:
r = next(it)
except StopIteration:
raise ValueError("Iterator is empty")
try:
next(it)
except StopIteration:
return r
else:
raise ValueError("Iterator has more than one item")
What do you think?
Thanks,Noam
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/D52MPKLIN4VEXBOCKVMTWAK66MAOEINY/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/3NA4E3QCTSLGIQXAMVWUL66TK6O7ZHLS/ Code of Conduct: http://python.org/psf/codeofconduct/