Python - Finding the index of Minimum Element in List


The position of the smallest value within a list is indicated by the index of the minimum element. Each list element has a distinct index that increases by one with each additional element after the first, starting at zero for the first element. By determining the index of the minimum element, we can locate the exact position of the smallest value within the list. In Python, we have some built-in functions like min(), index(), argmin(), lambda(), and, enumerate() to find the index of the minimum element in the list.

Syntax

The following syntax is used in the examples-

min()

The min() is a built-in function in Python that can be used to return the minimum value.

index()

The index() is a built-in function in Python that can be used to search the element from the list and return the position of the element.

argmin()

The built-in method argmin() of Python helps to find the minimum element and return the minimum position index.

lambda

This lambda function in Python is known as an anonymous function. It can be used when function objects are required.

enumerate()

The enumerate() is a built-in function in Python that allows keeping track of iterates over loops.

Using index() Function

The program uses index() to return the specific position/index of the element from the list. It also uses the built-in function min() to get the minimum index element.

Example

In the following example, start the program by using the function named find_min_index() that accepts the parameter lst( access the variable my_list through function call ). Then use the same parameter for the built-in function min() that will find the minimum value from the list and store it in the variable min_value. Now apply the built-in method index() with the variable lst to get the specific position index of minimum value that is present in the list. Next, use the function return min_index which returns the minimum index. Moving ahead to start creating the variable my_list that stores some integer values. Then use the function call in the variable min_index and use the same variable as a parameter of the print() function to get the result.

def find_min_index(lst):
    min_value = min(lst)
    min_index = lst.index(min_value)
    return min_index

# Create the list
my_list = [50, 2, 19, 11, 27]
min_index = find_min_index(my_list)
print(" The minimum index position:\n", min_index)

Output

 The minimum index position:
 1

Using enumerate()

The program uses built-in functions enumerate() to keep track of the iteration on the list and using for loop it helps to find the minimum index element.

Example

In the following example, the recursion technique is used to solve this problem statement. A list of numbers is passed as a parameter to the function find_min_index. Then min function is used within the function to get the minimum number in the input list. The code then iterates over the indices and values of the input list using a for loop and the enumerate function. The value of each index-value pair is compared to the minimum value. If the value is equal to the minimal value, the method returns the minimum element index in list.

def find_min_index(lst):
    min_value = min(lst)
    for index, value in enumerate(lst):
        if value == min_value:
            return index
# Create the list
my_list = [11, 12, 13, 14, 15]
min_index = find_min_index(my_list)
print("The minimum index position from the list:\n", min_index)

Output

 The minimum index position from the list:
 0

Using numpy.argmin() Function

The program uses the built-in function argmin() that follow the module named numpy to find both minimum elements with its index.

Example

In the following example, start the program with numpy module and take the reference object as np. Then it will use the recursive function to use some built-in functions such as array()[store the list elements of the same type] and argmin()[ return the minimum index with its element] and store it to their respective variables. Then use the function return to get the desired result.

import numpy as np

def find_min_index(lst):
    arr = np.array(lst)
    min_index = np.argmin(arr)
    return min_index

# create the list
my_list = [100, 676, 66, 987, 99, 111, 55, 444]
min_index = find_min_index(my_list)
print("The minimum index position from the list:\n", min_index)

Output

 The minimum index position from the list:
 6

Using min() with lambda Function

This is another method of this problem statement that uses three built-in functions to find the index of the minimum element in the list.

Example

In the following example, start the program with a function named find_min_index that accept the parameter lst to get the list of values. Then initialize the variable min_index that stores the value by using some built-in functions- min(), range(), len(), and lambda that will find the minimum index of the element in the list. Next, use the function call that variable my_list as a parameter and store it in the variable min_index. Finally, we are printing the result with the help of variable min_index.

def find_min_index(lst):
    min_index = min(range(len(lst)), key=lambda i: lst[i])
    return min_index

# Create the list
my_list = [5, 2, 9, 1, 7]
min_index = find_min_index(my_list)
print("The minimum index present at:\n", min_index)

Output

 The minimum index present at:
 3

Using sorted() Function

The program uses the built-in function sorted() and it accepts two parameters- enumerate() to keep track of iteration over list and key to set the built-in function lamda to sort the minimum element from the list.

Example

In the following example, there will be a given input list that passes through the function call. Then the function definition receives that list to work on the minimum index element by using the built-in function sorted(). Then use enumerate function within the function to build a list of tuples, each containing the index and value of an element from the input list. Sort this list of tuples in ascending order using the sorted function with a key parameter that specifies a lambda function to extract the value from each tuple. By function return, it will get the minimum value. Next, create the input list in the variable my_list. Then use the function call to pass the parameter of the list value i.e. my_list and store it in the variable min_index. Finally, use the same variable as a parameter of the print function to get the result.

def find_min_index(lst):
    sorted_list = sorted(enumerate(lst), key=lambda x: x[1])
    min_index = sorted_list[0][0]
    return min_index

# Create the list
my_list = [11, 7, 18, 20, 12, 0]
min_index = find_min_index(my_list)
print("The minimum index present at:\n",min_index)

Output

The minimum index present at:
 5

Conclusion

We discussed the various method to solve this problem statement where we learned some built-in functions of Python such as min(), array(), sorted(), enumerate(), index(), and, argmin(). This article helps us to build the logic based on searching the element in any search application.

Updated on: 16-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements