[Python-Dev] Function suggestion: itertools.one()

2020-07-27 Thread Noam Yorav-Raphael
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

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Rollo Konig-Brock
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 wrote: > >  > Hi, > > There's a simp

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Steven Barker
A single-name unpacking assignment can do exactly what you want, albeit with slightly less helpful exception messages: jack, = (p for p in people if p.id == '1234') # note comma after the name jack If no value is yielded by the generator expression, you'll get "ValueError: not enough values t

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Chris Angelico
On Tue, Jul 28, 2020 at 5:24 AM Steven Barker wrote: > > A single-name unpacking assignment can do exactly what you want, albeit with > slightly less helpful exception messages: > > jack, = (p for p in people if p.id == '1234') # note comma after the name > jack > Agreed. As a minor readabi

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Paul Ganssle
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 =

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Noam Yorav-Raphael
Thanks for the suggestion! I agree that using a list is clearer that having a trailing comma, I like it! I still think that having a one() function would be useful, since: 1. I think it spells the intention more clearly. Also the exception would be easier to understand, since errors in tuple unpac

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread David Mertz
I don't like consuming the iterator in the exception case. You might expect just one, but have a fallback approach for more. You could build the safer behavior using itertools.tee() or itertools.chain(). On Mon, Jul 27, 2020, 3:10 PM Noam Yorav-Raphael wrote: > Hi, > > There's a simple function

[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Guido van Rossum
Shouldn’t this go to itertools-ideas? :-) On Mon, Jul 27, 2020 at 13:56 David Mertz wrote: > I don't like consuming the iterator in the exception case. You might > expect just one, but have a fallback approach for more. You could build the > safer behavior using itertools.tee() or itertools.chai