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.
- Using min() method
- Using for loop
- Using pandas.Series.idxmin()
- Using numpy.array.argmin()
<a id="h-1-quick-examples-of-finding-index-of-min-value-of-list"></a>
1. Quick Examples of Finding Index of Min Value of List
If you are in a hurry, below are some quick examples of how to get the index position of the minimum element of the list.
# Quick examples of finding index of min value of list
# Consider the list of integers
marks = [82,31,40,78,90,32,120]
# Example 1: Using for loop
minimum_val= marks[0]
for i in range(1, len(marks)):
if (marks[i] < minimum_val):
minimum_val = marks[i]
result = marks.index(minimum_val)
# Example 2: Using index() method
result = marks.index(min(marks))
# Example 3: Using pandas.Series.idxmin()
import pandas as pd
result = pd.Series(assignment_marks).idxmin()
# Example 4: Using numpy.array.argmin()
import numpy as np
result = np.array(assignment_marks).argmin()
<a id="h-2-python-get-index-of-min-of-list"></a>
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.
<a id="h-2-1-syntax"></a>
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.
<a id="h-2-2-get-index-of-min-of-list-example"></a>
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
result = marks.index(min(marks))
print("Minimum index position:",result)
Yields below output.
Out of 7 integers, the minimum element is 12 and its index position is 0.
<a id="h-3-using-for-loop-amp-index-to-get-min-index"></a>
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.
<a id="h-3-1-for-loop-examples"></a>
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]
result = marks.index(minimum_val)
print("Minimum Value:",minimum_val)
print("Minimum Index position:",result)
Yields below output.
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.
<a id="h-4-using-series-idxmin-to-get-min-of-index"></a>
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.
<a id="h-4-1-syntax"></a>
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()
<a id="h-4-2-example"></a>
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()
result = pd.Series(assignment_marks).idxmin()
print("Minimum marks Index position:",result)
# Output:
# Minimum marks Index position: 0
Out of 6 integers, the minimum element is 31 and its index position is 0.
<a id="h-5-using-numpy-array-argmin"></a>
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.
<a id="h-5-1-syntax"></a>
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()
<a id="h-5-2-example"></a>
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()
result = np.array(assignment_marks).argmin()
print("Minimum marks Index position: ",result)
# Output:
# Minimum marks Index position: 4
Out of 6 integers, the minimum element is 32 and its index position is 4.
<a id="h-frequently-asked-questions-on-get-index-of-min-of-list-in-python"></a>
Frequently Asked Questions on Get Index of Min of List in Python
To get the index of the minimum value in a list in Python, you can use the index method along with the min function. For example, min(my_list) finds the minimum value in the list, and my_list.index(min(my_list)) returns the index of the first occurrence of that minimum value in the list.
If the list is empty, using the min function or the index method directly will raise a ValueError. You should check for an empty list before attempting to find the minimum value or its index.
The index method returns the index of the first occurrence of the specified value. If you want to find the index of the last occurrence, you can reverse the list and then use the index method.
You can use the numpy library to achieve this in a concise manner. The numpy library provides the argmin function, which returns the indices of the minimum values along a specified axis.
There are multiple approaches. In addition to the mentioned methods, you can also use a loop to iterate through the list and keep track of the minimum value and its index.
<a id="h-conclusion"></a>
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.
<a id="h-related-articles"></a>
Related Articles
- Python Join Lists (Two or Multiple Lists)
- Average of List in Python ( 7 Examples)
- Sum of Elements in the List in Python
- Python Find Item Index in List
- Convert List to String in Python
- Get Index of Max of List in Python
- Python Print List without Brackets
- Min Int in Python
- Python Append List to a List Example
- Python Flatten List of Lists with Examples
- Convert Range to List in Python
- Add Element to Front of List in Python
- Count Occurrences of Item in Python List
- Remove Multiple Items from List Python
