Index of Max and Min Values in Python

To get indexes of min and max values in a list, first, you have to use the min and max functions to find the smallest and largest numbers. Next, use the index function of the list to find out the index of these numbers.

This code will display the following result:

Index of min value is: 1
Index of max value is: 6

In the list, the smallest number is 1, and the largest is 78. Inside the list, each of these numbers occurs twice.

  • The smallest have indexes: 1 and 3.
  • The largest have indexes: 6 and 8.

The code will return only the first indexes, which are 1 and 6.

Get indexes of all min and max values

If you want to get all indexes of the smallest and largest values, you can search for min and max values and then use them inside the for loop to find all indexes.

Here’s how it works:

This code will add indexes of all smallest numbers into one list, and the largest into another. If you print them, you are going to get the following result.

Indexes of min value are: [1, 3]
Indexes of max value are: [6, 8]

Min and max indexes with Numpy

Another way you can get indexes is by using a module, called NumPy. This module is used for scientific computing in Python.

If you run this code, you are going to get the first indexes of min and max values.

Index of min value is: 1
Index of max value is: 6