Equivalent to list.index ?

Tim Hochberg tim.hochberg at ieee.org
Sun Nov 12 15:39:42 CST 2006

George Sakkis wrote:
> def index(a, value):
>     return N.where(a==value)[0][0]

def index(a, value):
    return argmax(a == value)

> This works but seems clunky and less efficient than necessary. If there
> isn't a better alternative, I would welcome a new index()
> function/method in the next numpy release.

Without commenting on the desirability of this particular function, 
people seem awfully eager to add functions and methods everywhere. These 
aren't free you know. They cost in terms of maintenance and potential 
bugs, and in terms of making the already large function set even harder 
to wrap ones head around. That's partly an organizational problem but 
partly an issue of sheer size, neither of which are helped by adding a 
bunch more stuff.

    Not every two-line Python function has to come pre-written 
        --Tim Peters

In the case of this particular function, what are the use cases? Are 
they common (not just with you but with other numpy users)? Are the uses 
speed critical? Is it a good building block for other numeric 
functions?  I'm skeptical of the above implementation as it doesn't fit 
well with other similar numpy functions (argmin and argmax for example) 
and I personally don't see much in the way of compelling uses for this, 
but feel free to try to convince me otherwise.


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list