In Python, we can easily find the index of the minimum in a list of numbers. The easiest way to get the index of the minimum in a list with a loop.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def minIndex(lst):
    index = [0]
    min = lst[0]
    for i in range(1,len(lst)):
        if lst[i] < min:
            min = lst[i]
            index = [i]
        elif lst[i] == min:
            index.append(i)
    return index

print(minIndex(list_of_numbers))

#Output:
[7]

You can also use enumerate() and list comprehension to get the index of the minimum of a list in Python.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]

min_of_list = min(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers ) if j == min_of_list]

print(index)
  
#Output:
[7]

There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.

One such task is to find the minimum of a list of numbers.

However, what if you want to find the index of the minimum?

We can easily find the index of the minimum value in a list using a loop in Python.

To find the index of the minimum of a list, we just need to keep track of the minimum and the index of the minimum, and loop over the list of numbers.

Below is an example of a Python function which gets the index of the minimum of a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def minIndex(lst):
    index = [0]
    min = lst[0]
    for i in range(1,len(lst)):
        if lst[i] < min:
            min = lst[i]
            index = [i]
        elif lst[i] == min:
            index.append(i)
    return index

print(minIndex(list_of_numbers))

#Output:
[7]

If you are using pandas, you can find the index of the minimum of a column or DataFrame with the pandas idxmin() function.

Using enumerate() and List Comprehension to Find Index of Minimum of List in Python

Another way we can get the position of the minimum of a list in Python is to use list comprehension and the enumerate() function.

The enumerate() function takes in a collection of elements and returns the values and indices of each item of the collection.

We can get use enumerate() and list comprehension to get the index of the minimum of a list by returning the index where any value is equal to the minimum of the list.

Below is how to use enumerate() and list comprehension to get the index of the minimum of a list in Python.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]

min_of_list = min(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers) if j == min_of_list]

print(index)
  
#Output:
[7]

Find Index of Maximum in List with Python

We can adjust the function which finds the index of the minimum in a list to find the index of the maximum in a list.

To find the index of the maximum of a list, we just need to keep track of the maximum and the index of the maximum, and loop over the list of numbers.

Below is an example of a Python function which gets the index of the maximum of a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def maxIndex(lst):
    index = [0]
    max = lst[0]
    for i in range(1,len(lst)):
        if lst[i] > max:
            max = lst[i]
            index = [i]
        elif lst[i] == max:
            index.append(i)
    return index

print(maxIndex(list_of_numbers))

#Output:
[2]

If you are using pandas, you can find the index of the maximum of a column or DataFrame with the pandas idxmax() function.

Hopefully this article has been useful for you to understand how to find the index of the minimum of a list in Python.

Categorized in:

Python,

Last Update: March 15, 2024