[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-23 Thread Stephen J. Turnbull
Christopher Barker writes: > Interesting -- in other recent threads, Ive felt that those of us that > thought "iterators and `next" were relatively advanced concepts that > newbies didn't need to learn were dismissed ... I for one don't *dismiss* that idea iterators and next are advanced, but

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-23 Thread Christopher Barker
> > for obj in somelist: > > > if comparison(obj, needle): > > > do_something(obj) > > > break > People who think in functional programming terms will probably love the > `next(filter(...))` idiom, but not everyone thinks or likes functional > programming

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-23 Thread Dan Sommers
On Saturday, May 23, 2020, at 11:02 -0400, David Mertz wrote: > Still, generator comprehension are great. And next() is an excellent > function. Agreed, on both counts. I often end up needing an arbitrary element of a set (or the only element of a single-element set), and next(iter(set))

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-23 Thread David Mertz
On Sat, May 23, 2020, 10:54 AM Rob Cliffe via Python-ideas > index_of(needle, haystack, key=func) > > Sounds like a list comprehension: [ needle for needle in haystack if > func(needle) ] > The times it doesn't sound like a list comprehension is when you have a million items in the list, 100k of

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-23 Thread Rob Cliffe via Python-ideas
On 23/05/2020 05:48, David Mertz wrote: On Sat, May 23, 2020, 12:26 AM Steven D'Aprano Obviously not all such key functions are that simple and you may need to write a helper function, but the same applies to filter. I like the key function much better than the predicate. In large

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread David Mertz
On Sat, May 23, 2020, 12:26 AM Steven D'Aprano > Obviously not all such key functions are that simple and you may need to > write a helper function, but the same applies to filter. > I like the key function much better than the predicate. In large part that's because as soon as you say

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread Steven D'Aprano
On Fri, May 22, 2020 at 06:37:11PM -0700, Ethan Furman wrote: > On 05/22/2020 05:11 PM, David Mertz wrote: > >On 05/22/2020 04:43 AM, Steven D'Aprano wrote: > > >>i = somelist.index(needle, pred=comparison) > > >Why not just this (by object, not by its index, but that seems simpler): > > > >

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread David Mertz
On Fri, May 22, 2020 at 8:59 PM Steven D'Aprano wrote: > > Why not just this (by object, not by its index, but that seems simpler): > > >>> do_something(next(filter(pred, somelist))) > > Sure, that works too. But have you ever written it for real? I haven't. > And having seen it, I'll probably

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread Ethan Furman
On 05/22/2020 05:11 PM, David Mertz wrote: On 05/22/2020 04:43 AM, Steven D'Aprano wrote: i = somelist.index(needle, pred=comparison) Why not just this (by object, not by its index, but that seems simpler): >>> do_something(next(filter(pred, somelist))) Something about 55 >>>

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread Steven D'Aprano
On Fri, May 22, 2020 at 08:11:16PM -0400, David Mertz wrote: > > > > After I answered that question, it dawned on me that I have probably > > written something like that loop, or variations of it, a thousand times: > > > > for obj in somelist: > > if comparison(obj, needle): > >

[Python-ideas] Re: Enhance list.index with a comparison function

2020-05-22 Thread David Mertz
> > After I answered that question, it dawned on me that I have probably > written something like that loop, or variations of it, a thousand times: > > for obj in somelist: > if comparison(obj, needle): > do_something(obj) > break > Why not just this (by