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 vote

Best answer

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])

You can also use **argmax()** and **argmin()** functions to find the index of maximum and minimum values respectively.

E.g.

>>> import numpy as np

>>> x=np.array([12,14,6,18,20,2])

>>> np.argmin(x)

5

>>> np.argmax(x)

4