# Find Index of Minimum Element in a List in Python

Manav Narula Oct 10, 2023

A list object in Python emulates an array and stores different elements under a common name. Elements are stored at a particular index which we can use to access them.

We can perform different operations with a list. It is straightforward to use built-in list functions like max(), min(), len, and more to return the maximum element, smallest element, and list length.

This article will find the minimum element index in a Python list.

## Use the min() and index() Functions to Find the Index of the Minimum Element in a List in Python

In Python, we can use the min() function to find the smallest item in the iterable. Then, the index() function of the list can return the index of any given element in the list.

A ValueError is raised if the given element is not in the list.

Example:

lst = [8, 6, 9, -1, 2, 0]
m = min(lst)
print(lst.index(m))

Output:

3

Remember, the index of a list starts from 0. The above answer shows 3 since the smallest element is in the fourth position.

## Use the min() Function and for Loop to Find the Index of the Minimum Element in a List in Python

We can substitute the use of the index() function in the previous method with a for loop. It can iterate over the list and compare elements individually.

When there is a match, we return the value of that index and break out of the loop using the break statement.

Example:

lst = [8, 6, 9, -1, 2, 0]
m = min(lst)
for i in range(len(lst)):
if lst[i] == m:
print(i)
break

Output:

3

## Use the min() and enumerate() Functions to Find the Index of the Minimum Element in a List in Python

The enumerate() function accepts an iterable. It returns an object containing the elements of the iterable with a counter variable for the element index.

This object can be iterated using a for loop. Then, we will iterate over this object using list comprehension, create a new list, and use the min() function to locate the minimum element in a list.

We will get the element and its index in one line.

Example:

lst = [8, 6, 9, -1, 2, 0]
a, i = min((a, i) for (i, a) in enumerate(lst))
print(i)

Output:

3

## Use the min() and operator.itemgetter() Functions to Find the Index of the Minimum Element in a List in Python

The operator module in Python provides additional operators which we can use to simplify our code. The itemgetter() function from this module returns a callable object and can retrieve some element from its operand.

The min() function accepts a key parameter to determine the criteria for the comparison. We can provide the itemgetter() function as the value for this parameter to return the index of the minimum element.

Example:

from operator import itemgetter

lst = [8, 6, 9, -1, 2, 0]
i = min(enumerate(lst), key=itemgetter(1))[0]
print(i)

Output:

3

We first find the minimum element and its index in the previous methods. This method does both these steps in one line; therefore, it is considered a faster approach.

## Use the min() and __getitem__() Functions to Find the Index of the Minimum Element in a List in Python

The operator.itemgetter() function calls the magic function __getitem__() internally. We can avoid the need for importing the operator module by directly working with this function and improving the speed of the code.

It is similar to the previous method to return the minimum element index in a list in Python.

Example:

lst = [8, 6, 9, -1, 2, 0]
i = min(range(len(lst)), key=lst.__getitem__)
print(i)

Output:

3

## Use the numpy.argmin() Function to Find the Index of the Minimum Element in a List in Python

The numpy.argmin() function is used to find the position of the smallest element in Python. We can use this function with lists, and it will return an array with the indices of the minimum element of the list.

Example:

import numpy as np

lst = [8, 6, 9, -1, 2, 0]
i = np.argmin(lst)
print(i)

Output:

3

## Conclusion

To wrap up, we discussed several methods to find the index of the minimum element in a list. The min() function was the most common among all the methods.

Different functions like enumerate(), itemgetter(), and more can be used to create different approaches. The final method, using the numpy.argmin() function, is more straightforward.

Author: Manav Narula

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.