find index of minimum value in list python

This tutorial is about how to find the Index of the minimum value in the list using python. We are assuming that you’re already familiar with the list, its syntax, and its uses. We also discuss the index of the maximum value in the list.

If you want to learn more about lists in Python, See Python List Tutorials

Given a list of N integers, find the index of elements whose value is minimum among all the elements in the list. There are two cases which are given below:

Case 1: When there is only one minimum value in the list.
For example:
Input List:  23,56,32,89,21,44,51
Output : The index of minimum value is 5.

Case 2: When the minimum value is repeated multiple times in the list.
For example:
Input List:  32,23,56,32,89,41,32,23,44,51,23
Output : The minimum value occurs at 2nd, 8th and 11th indices.

Let’s discuss the above-mentioned two cases using different appr3oaches along with examples.

Index of minimum value using min() and index() Python functions

The python has a built-in function of min() which returns the minimum value in the list and index() which returns the index of the element. These two functions can be used to find the index of a minimum element in a single line code.

Example 1:

Given a list, find the index of a minimum element.

#function which returns the index of minimum value in the list

def get_minvalue(inputlist):

    #get the minimum value in the list
    min_value = min(inputlist)

    #return the index of minimum value 
    min_index=list1.index(min_value)
    return min_index


if __name__ == "__main__" :

    #create and initialize a list

    list1 = [23,56,32,89,21,44,51]

    list2 = [32,23,56,32,89,41,32,23,44,51,23]

    min_list1 = get_minvalue(list1)

    print("Index of minimum value is ",min_list1)

    min_list2 = get_minvalue(list2)

    print("Index of minimum value is ",min_list2)
 

Output:

output after Finding Index Of Minimum Value In List using python Python

In the above example, there is only one minimum value in list 1 whose index is returned whereas in list 2, minimum value occurs more than once but in the output, only the index 0 is returned. This is because, in the case of multiple occurrences of an item, the index() function returns the index of only the first occurrence. This approach works well for case 1 but fails to display all the indices in case of multiple occurrences.

Approach 2: using min() and For Loop:

The second approach is based on the combination of min() and for a loop. This approach overcomes the limitation of the previous approach. It is able to return all the indices in case of multiple occurrences of the same elements. First of all, we acquire the minimum element using the min() command. Then iterate over the list using for loop and store the indexes of all those positions whose value is equal to the minimum value. Let’s understand this through an example.

Example:

#function which returns the index of minimum value in the list

def get_minvalue(inputlist):

    #get the minimum value in the list
    min_value = min(inputlist)

    #return the index of minimum value 

    min_index=[]

    for i in range(0,len(inputlist)):

      if min_value == inputlist[i]:
        min_index.append(i)

    return min_index


if __name__ == "__main__" :

    #create and initialize a list
    list1 = [23,56,32,89,21,44,51]

    list2 = [32,23,56,32,89,41,32,23,44,51,23]

    min_list1 = get_minvalue(list1)
    print("Index of minimum value is ",min_list1)

    min_list2 = get_minvalue(list2)
    print("Index of minimum value is ",min_list2)
 

Output:

another output of Index Of Minimum Value In List Python

Now, you can see that the code has returned all the indices of the minimum value in the list2. In the above example, you can also use enumerate function in place of the range function. The results would be the same. This code can be further modified by using list comprehension which in turn reduces the length of code. Below is the implementation of the above code using list comprehension.

Example:

#function which returns the index of minimum value in the list

def get_minvalue(inputlist):

    #get the minimum value in the list
    min_value = min(inputlist)

    #return the index of minimum value 
    res = [i for i,val in enumerate(inputlist) if val==min_value]
    return res


if __name__ == "__main__" :

    #create and initialize a list

    list1 = [23,56,32,89,21,44,51]
    list2 = [32,23,56,32,89,41,32,23,44,51,23]

    min_list1 = get_minvalue(list1)
    print("Indices of minimum value: ",min_list1)

    min_list2 = get_minvalue(list2)
    print("Indices of minimum value: ",min_list2)
 

Output:

The above code uses list comprehension and returns the same results as in the previous example.

This tutorial discusses two different approaches to find the indices of minimum values in the list along with their limitations. If you have any questions regarding this tutorial, let us know in the comments. It would be highly appreciated.

See more Python Tutorials

Leave a Comment

Your email address will not be published.