[Python-ideas] list.index() extension

Steven D'Aprano steve at pearwood.info
Sun Apr 5 04:52:23 CEST 2009

On Sun, 5 Apr 2009 09:36:51 am Greg Ewing wrote:
> Christian Heimes wrote:
> >>def index(self, obj, predicate=operator.eq):
> >>    for idx, item in enumerate(self):
> >>        if predicate(item, obj):
> >>            return idx
> >>    raise IndexError
> This looks more like it belongs in the itertools module,
> if there isn't something there already that does it.

Not to me. Surely itertools is for functions which return iterators, not 
arbitrary functions that take an iterable argument?

It certainly does look like a useful function to have, but I don't know 
where in the standard library it should go, given that Guido dislikes 
grab-bag modules of miscellaneous functions. To my mind, that makes it 
a candidate to become a list/tuple/str method.

I like Christian's suggestion. I've often wished for a built-in way to 
do element testing by identity instead of equality, and by "often" I 
mean occasionally. So I'm +1 on the proposal.

Steven D'Aprano

More information about the Python-ideas mailing list