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

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

Muhammad Maisam Abbas Nov-26, 2021 Mar-04, 2021 Python Python List
  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

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.

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

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.

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

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.

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.

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.

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.

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

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python