How to Get the Index of the Minimum Element of a List in Python [6 Methods]

In this Python article, I will explain how to get the index of the minimum element of a list in Python. Here, I will describe the six ways to get the index of the minimum element of a list with illustrative examples.

Methods demonstrated in this article to get the index of the minimum element of a list in Python:

Note: We use the min() method in Python to find the minimum valued element in the list. The min() method returns an element with the minimum value from the list, and we can combine it with different functions to get the index of the min element of a list in Python.

  • Using index() methods
  • Using lambda function
  • Using enumerate() methods
  • Using sorted() function
  • Using for loop
  • Using itemgetter() methods

Python program to get the index of the minimum element of the list index() function

The index() function in Python is another built-in function specifically used for finding the index of the first occurrence of a given value in a sequence.

We can combine this with the min() function to get the last index of the minimum element of the list in Python.

READ:  Python Program to Get Last Day of Month [5 Ways]

Here is the full code:

marks = [100, 67, 88, 29, 33, 74]
result = marks.index(min(marks))

print("Minimum index position:",result)

Output:

marks = [100, 67, 88, 29, 33, 74]

result = marks.index(min(marks))
print("Minimum index position:",result) 

Get the index of the minimum element of a list in Python using min() and lambda function

This is another approach to get the index of the minimum element of a list in Python using lambda functions.

In Python, the lambda functions are similar to user-defined functions but without a name. They are commonly referred to as anonymous functions.

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

price_list = [5, 2, 9, 1, 7]
minimum_index = find_min_index(price_list)
print("The minimum index present at:", minimum_index)

This line of Python code finds the index of the minimum value in the list using the min function along with the range function to iterate over indices and a lambda function to determine the key for comparison.

minimum_index = min(range(len(list)), key=lambda i: list[i])

Output:

The minimum index present at: 3

Refer to the image below to see the output of the Python program.

Finding the index of Minimum Element in List in Python

Get the index of the minimum element of a list in Python using the enumerate() function

In Python, the enumerate() function takes an input as an iterable, adds a counter to each iterable element, and returns an enumerate object. The counter can also act as an index to each element, which can be used as an element.

This is the complete program to get the index of the minimum element of a list in Python using the min() and enumerate() functions.

def get_min_index(list):
    minimum_value = min(list)
    for index, value in enumerate(list):
        if value == minimum_value:
            return index
temperatures = [55, 36, 42, 35, 68, 45]
minimum_index = get_min_index(temperatures)
print("The minimum index position from the list:", minimum_index)

Here, this function calculates the minimum value in the given list using the min() function, then iterates over the elements and indices of the list using enumerate(). Within the loop, it compares each value with the minimum value and returns the index if it matches.

def get_min_index(list):
    minimum_value = min(list)
    for index, value in enumerate(list):
        if value == minimum_value:

Output:

The minimum index position from the list: 3

Refer to the screenshot below, it gave the output of the above Python program.

Getting the index of the returned min item using min() on a list in Python

Python program to get the index of the minimum element of the list using the sorted() function

We can use the enumerate() in Python to get tuples of (index, value) pairs, then sort these pairs based on the values using the sorted() function with a custom key function.

READ:  How to Create String with Single Quotes in Python

Finally, the function will return the index of the first element in the sorted list of indices, corresponding to the index of the minimum element in the original list in Python.

Below is the Python source code to get the index of the minimum element of the list using the sorted().

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

input_list = [21, 7, 15, 2, 11, 0]
output_index = get_min_index(input_list)
print("The minimum index present is:",output_index)

Output:

The minimum index present is: 5

You can refer to the screenshot below:

Get Index of Min of List in Python

How to get the index of the minimum element of the list in Python using a for loop

This is another way to get the index of the minimum element of the list in Python using a for loop.

This is the complete code to get the index of the minimum element of the list in Python using a for loop.

input_number_list = [62, 31, 20, 88, 49, 12, 120]
minimum_value= input_number_list[0]

for i in range(1, len(input_number_list)): 
    if (input_number_list[i] < minimum_value):
        minimum_value = input_number_list[i]
result = input_number_list.index(minimum_value)

print("Minimum Value:",minimum_value)
print("Minimum Index position:",result)

These lines of Python code iterate over the list indices starting from 1. Then, it compares each element with the minimum value, updates if a smaller value is found, and retrieves the index of the value using the index() method.

for i in range(1, len(input_number_list)): 
    if (input_number_list[i] < minimum_value):
        minimum_value = input_number_list[i]
result = input_number_list.index(minimum_value)

Output:

Minimum Value: 12
Minimum Index position: 5

The image below shows the output of the above Python code.

How to Find Index of Minimum Value in List in Python

Get the index of the minimum element of a list in Python using min() and itemgetter()

We can get the index of the minimum element of a list in Python using the min() and itemgetter() functions.

READ:  Python Dictionary Count using Various Methods

In Python, the itemgetter() function from the operator module returns a callable object and can get some element from its operand.

To achieve this, I will use this Python program.

from operator import itemgetter

number_list = [18, 16, 19, 11, 12, 10]
result = min(enumerate(number_list), key=itemgetter(1))[0]
print("Index of minimum value is:",result) 

Output:

Index of minimum value is: 5

The screenshot below shows the output of the Python code to get the index of the minimum element of a list using min() and itemgetter().

Python Find Index of Minimum in List

Conclusion

In this Python article, the information provided gives you a clearer picture of how to get the index of the minimum element of a list in Python.

Additionally, I have covered different ways to get the index of the minimum element of a list in Python, such as using min() with index(), min() with lambda(), min() and enumerate(), using sorted(), for loop, and min() and itemgetter().

By understanding these methods, developers can choose according to their needs.

You may also like to read these articles: