ID : 295

viewed : 73

Tags : PythonPython List

92

In this tutorial, we will discuss methods to get the index of the maximum and minimum values of a list in Python.

`max()`

and `list.index()`

Functions in PythonThe gives the maximum value in a list in Python. The gives the index of `x`

in the list. The following code example shows us how we can get the index of the maximum value of a list with the `max()`

and `list.index()`

functions in Python.

`list1 = [10, 12, 13, 0, 14] tmp = max(list1) index = list1.index(tmp) print(index) `

Output:

`4 `

In the above code, we first get the maximum value inside the list `list1`

with the `max()`

function and store it in `tmp`

and then get the maximum value’s index by passing `tmp`

to the `list1.index()`

method. The above code can be shortened if we only want to display the index of the maximum value.

`list1 = [10, 12, 13, 0, 14] print(list1.index(max(list1))) `

Output:

`4 `

`min()`

and `list.index()`

Functions in PythonThe gives the minimum value in a list in Python. The `list.index(x)`

method has already been discussed in the previous section. The following code example shows us how we can get the index of the minimum value of a list with the `min()`

and `list.index()`

functions in Python.

`list1 = [10, 12, 13, 0, 14] tmp = min(list1) index = list1.index(tmp) print(index) `

Output:

`3 `

In the above code, we first get the smallest value inside the list `list1`

with the `min()`

function and store it in `tmp`

. And then we get the minimum value’s index by passing `tmp`

to the `list1.index()`

function. The above code can be shortened if we only want to display the index of the minimum value.

`list1 = [10, 12, 13, 0, 14] print(list1.index(min(list1))) `

Output:

`3 `

`numpy.argmax()`

Function in PythonThe in the `NumPy`

package gives us the index of the maximum value in the list or array passed as an argument to the function. The following code example shows us how we can get the index of the maximum value of a list with the `numpy.argmax()`

function in Python.

`import numpy list1 = [10, 12, 13, 0, 14] maxindex = numpy.argmax(list1) print(maxindex) `

Output:

`4 `

In the above code, we get the index of the maximum value in the list `list1`

with the `numpy.argmax()`

function.

`numpy.argmin()`

Function in PythonThe in the `NumPy`

package gives us the index of the minimum value in the list or array passed as an argument to the function. The following code example shows us how we can get the index of the minimum value of a list with the `numpy.argmin()`

function in Python.

`import numpy list1 = [10, 12, 13, 0, 14] minindex = numpy.argmin(list1) print(minindex) `

Output:

`3 `

In the above code, we get the index of the minimum value in the list `list1`

with the `numpy.argmin()`

function.