How to Get Index of Maximum and Minimum Value of a List in Python

  1. Get Index of the Maximum Value of a List With the max() and list.index() Functions in Python
  2. Get Index of the Minimum Value of a List With the min() and list.index() Functions in Python
  3. Get Index of the Maximum Value of a List With the numpy.argmax() Function in Python
  4. Get Index of the Minimum Value of a List With the numpy.argmin() Function in Python
How to Get Index of Maximum and Minimum Value of a List in Python

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

Get Index of the Maximum Value of a List With the max() and list.index() Functions in Python

The max() function gives the maximum value in a list in Python. The list.index(x) method 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.

Python
 pythonCopylist1 = [10, 12, 13, 0, 14]

tmp = max(list1)
index = list1.index(tmp)

print(index)

Output:

 textCopy4

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.

Python
 pythonCopylist1 = [10, 12, 13, 0, 14]

print(list1.index(max(list1)))

Output:

 textCopy4

Get Index of the Minimum Value of a List With the min() and list.index() Functions in Python

The min() function 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.

Python
 pythonCopylist1 = [10, 12, 13, 0, 14]

tmp = min(list1)
index = list1.index(tmp)

print(index)

Output:

 textCopy3

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.

Python
 pythonCopylist1 = [10, 12, 13, 0, 14]

print(list1.index(min(list1)))

Output:

 textCopy3

Get Index of the Maximum Value of a List With the numpy.argmax() Function in Python

The numpy.argmax() function 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.

Python
 pythonCopyimport numpy

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

print(maxindex)

Output:

 textCopy4

In the above code, we get the index of the maximum value in the list list1 with the numpy.argmax() function.

Get Index of the Minimum Value of a List With the numpy.argmin() Function in Python

The numpy.argmin() function 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.

Python
 pythonCopyimport numpy

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

print(minindex)

Output:

 textCopy3

In the above code, we get the index of the minimum value in the list list1 with the numpy.argmin() function.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python List