+2 votes
in Programming Languages by (15.7k points)
I want to find the index of the maximum/minimum elements in a list or Numpy array. What function should I use? I do not want to use loop to find min and max.

1 Answer

0 votes
by (27.1k points)

Numpy's where() function can be used to find the index of the maximum or minimum element in a list or NumPy array.

Here is an example:

    >>> import numpy as np
    >>> x=np.array([12,14,6,18,20,2])
    >>> np.where(x == np.max(x))[0]   #for max
    array([4])
    >>> np.where(x == np.min(x))[0]  #for min
    array([5])

...