Get Index of Min of List in Python

How to get the index of a min (minimum) element of the list in Python? If you want to return the minimum element index position from the given list, you can use the min() to get the minimum value and use this with the index() method to get the index position of the minimum element. Apart from this method, we will discuss some other methods that will return the minimum element index position.

  1. Using min() method
  2. Using for loop
  3. Using pandas.Series.idxmin()
  4. Using numpy.array.argmin()

1. Quick Examples of Finding Index of Min Value of List

Following are quick examples of how to get the index position of the minimum element of the list.


# Quick examples

# Consider the list of integers
marks = [82,31,40,78,90,32,120]
 
# Using for loop
minimum_val= marks[0]
for i in range(1, len(marks)): 
    if (marks[i] < minimum_val):
        minimum_val = marks[i]
print("Minimum Index position: ",marks.index(minimum_val))

# Using index() method
print("Minimum Index position: ",marks.index(min(marks))) 

# Using pandas.Series.idxmin()
import pandas as pd
print("Minimum Index position: ",pd.Series(marks).idxmin()) 

# Using numpy.array.argmin()
import numpy as np
print("Minimum Index position: ",np.array(marks).argmin())

2. Python Get Index of min() of List

We can use the Python min() function to get the minimum element and use the list.index() method to get the index position of the minimum element by passing the minimum value to the index() method. The index() returns the index position of the input value from the list.

2.1 Syntax

Let’s look at the syntax of how to use min() with index().


# Syntax
mylist1.index(min(mylist1))

Here, mylist1 is the input list.

2.2 Get Index of Min of List Example

Let’s create a list of integers and return the index position of the minimum element. Here, the min(marks) returns the minimum value of the python list, and the index(min(marks)) returns the index position of the minimum value from the list.


# Consider the list of integers
marks = [12,31,40,78,90,32,120]
 
# Using index() method
print("Minimum Index position: ",marks.index(min(marks)))  

# Output:
# Minimum Index position:  0

Out of 7 integers, the minimum element is 12 and its index position is 0.

3. Using for loop & index() to get Min Index

Here, we will iterate all elements in the list and compare whether the element is minimum to the current iterating value, If it is minimum, we will store this minimum value in a variable, and finally using the index() method we will return its index position in the list.

3.1 for loop Examples

Example 1: Let’s create a list of integers and return the index position of the minimum element.


# Consider the list of integers
marks = [82,31,40,78,90,32,120]
 
# Using for loop
minimum_val= marks[0]
for i in range(1, len(marks)): 
    if (marks[i] < minimum_val):
        minimum_val = marks[i]

print("Minimum Value:",minimum_val)
print("Minimum Index position: ",marks.index(minimum_val))  

# Output:
# Minimum Value: 31
# Minimum Index position:  1

Out of 7 integers, the minimum element is 31 and its index position is 1.

Example 2: Let’s create a list of 3 strings and return the index position of the minimum element.


# Consider the list of strings
languages = ["punjabi","tamil","malayalam"]
 
# Using for loop
minimum_element= languages[0]
for i in range(1, len(languages)): 
    if (languages[i] < minimum_element):
        minimum_element = languages[i]

print("Minimum Index position: ",languages.index(minimum_element))  

# Output:
# Minimum Index position:  2

Among the 3 languages, “malayalam” is minimum (ASCII value of m is smaller than the other two elements), So its index position is returned, i.e 2.

4. Using Series.idxmin() to get Min of Index

The pandas is a module that is used for data analysis available in python. In this module, a Series is a Data structure that will hold elements in a linear fashion. It has idxmin() method which will return the index position of the minimum element present in the Series.

In order to use the Series, first you need to convert the list to a series by passing our list to the series() and apply idxmin() method to return the index position of minimum value.

4.1 Syntax

Let’s look at the syntax of how to use pandas.Series.idxmin()


# Here, mylist1 is the input list.
pd.Series(mylist1).idxmin()

4.2 Example

Let’s create a list of integers and return the index position of the minimum element using idxmin().


import pandas as pd
# Consider the list of integers
assignment_marks = [31,40,78,90,32,120]
 
# Using pandas.Series.idxmin()
print("Minimum marks Index position: ",pd.Series(assignment_marks).idxmin())  

# Output:
# Minimum marks Index position:  0

Out of 6 integers, the minimum element is 31 and its index position is 0.

5. Using numpy.array().argmin()

The NumPy in python is a module that will perform mathematical operations on arrays. Here, argmin() is a method supported by numpy that will return the index of the minimum element in the numpy array. To use NumPy, you need to install it first and import it.

We can utilize this method by converting our list to the numpy array.

5.1 Syntax

Let’s look at the syntax of how to use numpy.array().argmin()


# Here, mylist1 is the input list.
np.array(mylist1).argmin()

5.2 Example

Let’s create a list of integers and return the index position of the minimum element using numpy.argmin().


import numpy as np
# Consider the list of integers
assignment_marks = [91,40,78,90,32,120]
 
# Using numpy.array.argmin()
print("Minimum marks Index position: ",np.array(assignment_marks).argmin())

# Output:
# Minimum marks Index position:  4

Out of 6 integers, the minimum element is 32 and its index position is 4.

6. Conclusion

In this article, you have learned how to return the minimum value index position from the Python list using 4 different approaches. First, we discussed using the for loop & index() and then directly using min() & index(). It can be possible to use idxmin() by converting our list to pandas Series and argmin() by converting our list to the numpy array.

Leave a Reply