Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of minimum element of list. This task is easy and discussed many times. But sometimes, we can have multiple minimum elements and hence multiple minimum positions. Let’s discuss ways to achieve this task.
Method #1: Using min() + enumerate() + list comprehension In this method, the combination of above functions is used to perform this particular task. This is performed in two steps. In 1st, we acquire the minimum element and then access the list using list comprehension and corresponding element using enumerate and extract every element position equal to minimum element processed in step 1.
Python3
test_list
=
[
2
,
5
,
6
,
2
,
3
,
2
]
print
("The original
list
: "
+
str
(test_list))
temp
=
min
(test_list)
res
=
[i
for
i, j
in
enumerate
(test_list)
if
j
=
=
temp]
print
("The Positions of minimum element : "
+
str
(res))
Output :
The original list : [2, 5, 6, 2, 3, 2] The Positions of minimum element : [0, 3, 5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using loop + min() This is brute method to perform this task. In this, we compute the minimum element and then iterate the list to equate to min element and store indices.
Python3
test_list
=
[
2
,
5
,
6
,
2
,
3
,
2
]
print
("The original
list
: "
+
str
(test_list))
temp
=
min
(test_list)
res
=
[]
for
idx
in
range
(
0
,
len
(test_list)):
if
temp
=
=
test_list[idx]:
res.append(idx)
print
("The Positions of minimum element : "
+
str
(res))
Output :
The original list : [2, 5, 6, 2, 3, 2] The Positions of minimum element : [0, 3, 5]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Approach 3: Using numpy
Note: Install numpy module using command “pip install numpy”
The numpy.where() function returns the indices of elements in an array that satisfy a given condition. In this case, the condition is test_list == np.min(test_list), which returns a Boolean array with True at the indices where the elements are equal to the minimum element in the list, and False elsewhere. The [0] at the end is used to extract the indices from the output of numpy.where(), which is a tuple containing the indices in the first element.
Python3
import
numpy as np
test_list
=
[
2
,
5
,
6
,
2
,
3
,
2
]
print
(
"The original list : "
+
str
(test_list))
res
=
np.where(test_list
=
=
np.
min
(test_list))[
0
]
print
(
"The Positions of minimum element : "
+
str
(res))
Output:
The original list : [2, 5, 6, 2, 3, 2] The Positions of minimum element : [0 3 5]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Use a dictionary to store the indices of each unique value in the list.
Step-by-step approach
- Define the input list test_list with some integer values.
- Print the input list using print().
- Find the minimum value in the list using the min() function, and store it in the variable min_val.
- Create an empty dictionary index_dict to store the indices of each unique value in the list.
- Loop through the elements in test_list using the enumerate() function to get both the index and value at each position in the list.
- Check if the current value is already in index_dict. If it is not, add a new key-value pair to the dictionary where the key is the value and the value is a list
- containing the current index. If the value is already in the dictionary, append the current index to the list of indices for that value.
- Retrieve the list of indices for the minimum value from index_dict and store it in the variable res.
- Print the list of indices of the minimum element in the original list using print().
Below is the implementation of the above approach:
Python3
test_list
=
[
2
,
5
,
6
,
2
,
3
,
2
]
print
(
"The original list : "
+
str
(test_list))
min_val
=
min
(test_list)
index_dict
=
{}
for
i, x
in
enumerate
(test_list):
if
x
not
in
index_dict:
index_dict[x]
=
[i]
else
:
index_dict[x].append(i)
res
=
index_dict[min_val]
print
(
"The Positions of minimum element : "
+
str
(res))
Output
The original list : [2, 5, 6, 2, 3, 2] The Positions of minimum element : [0, 3, 5]
Time complexity: O(n), where n is the length of the list, because it loops through the list once to build the dictionary and once to retrieve the indices of the minimum value.
Auxiliary space: O(m), where m is the number of unique values in the list, because the dictionary can potentially store indices for each unique value in the list.
Last Updated :
17 Apr, 2023
Like Article
Save Article
In this tutorial, we will look at how to find the min value in a Python list and its corresponding index with the help of some examples.
How to get the minimum value in a list in Python?
A simple approach is to iterate through the list and keep track of the minimum value. Alternatively, you can also use the Python built-in min()
function to find the minimum value in a list.
Let’s look at some of the different ways of finding the smallest value and its index in a list.
Loop through the list to find the minimum
Iterate through the list values and keep track of the min value. Here’s an example.
# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] for val in ls: if val < min_val: min_val = val # display the min value print(min_val)
Output:
1
Here, we iterate over each value in the list ls
and keep track of the minimum value encountered in the variable min_val
. After the loop finishes, the variable min_val
stores the minimum value present in the list, 1.
You can use this method to get the index corresponding to the minimum value in the list as well. Use an additional variable to keep track of the current minimum value’s index.
# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] min_val_idx = 0 for i in range(len(ls)): if ls[i] < min_val: min_val = ls[i] min_val_idx = i # display the min value print(min_val) # display its index print(min_val_idx)
Output:
1 4
We get the minimum value and its index after the loop finishes. Here we iterate through the list via its index rather than the values. You can also use the enumerate()
function to iterate through the index and value together.
Using min()
to get the maximum value
You can also use the Python built-in min()
function to get the min value in a list. The function returns the minimum value in the passed iterable (for example, list, tuple, etc.).
# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min(ls)
Output:
1
Using the min()
function is simple and is just a single line code compared to the previous example.
You can use the list index()
function to find the index corresponding to the minimum value (assuming you already know the minimum value).
# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min_val = min(ls) # display the min value print(min_val) # display its index print(ls.index(min_val))
Output:
1 4
We get the min value and its index in the list ls
.
Note that the list index()
function returns the index of the first occurrence of the passed value. If the min value occurs more than once in the list, you’ll only get the index of its first occurrence. You can use list comprehension to get all the indices of occurrence of the min value in the list.
# create a list ls = [3, 6, 1, 2, 1, 5] # find min value min_val = min(ls) print(min_val) # find all indices corresponding to min val min_val_idx = [i for i in range(len(ls)) if ls[i]==min_val] print(min_val_idx)
Output:
1 [2, 4]
We get all the indices where the minimum value occurs in the list ls
.
You might also be interested in –
- Find Mode of List in Python
- Python – Get median of a List
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
-
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts
- Use the
min()
andindex()
Functions to Find the Index of the Minimum Element in a List in Python - Use the
min()
Function andfor
Loop to Find the Index of the Minimum Element in a List in Python - Use the
min()
andenumerate()
Functions to Find the Index of the Minimum Element in a List in Python - Use the
min()
andoperator.itemgetter()
Functions to Find the Index of the Minimum Element in a List in Python - Use the
min()
and__getitem__()
Functions to Find the Index of the Minimum Element in a List in Python - Use the
numpy.argmin()
Function to Find the Index of the Minimum Element in a List in Python - Conclusion
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:
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:
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:
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:
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:
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:
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.
Say that you have a list values = [3,6,1,5]
, and need the index of the smallest element, i.e. index_min = 2
in this case.
Avoid the solution with itemgetter()
presented in the other answers, and use instead
index_min = min(range(len(values)), key=values.__getitem__)
because it doesn’t require to import operator
nor to use enumerate
, and it is always faster(benchmark below) than a solution using itemgetter()
.
If you are dealing with numpy arrays or can afford numpy
as a dependency, consider also using
import numpy as np
index_min = np.argmin(values)
This will be faster than the first solution even if you apply it to a pure Python list if:
- it is larger than a few elements (about 2**4 elements on my machine)
- you can afford the memory copy from a pure list to a
numpy
array
as this benchmark points out:
I have run the benchmark on my machine with python 2.7 for the two solutions above (blue: pure python, first solution) (red, numpy solution) and for the standard solution based on itemgetter()
(black, reference solution).
The same benchmark with python 3.5 showed that the methods compare exactly the same of the python 2.7 case presented above
answered Aug 6, 2012 at 9:43
6
if is_min_level:
return values.index(min(values))
else:
return values.index(max(values))
answered Mar 18, 2010 at 23:23
too much phptoo much php
88.1k34 gold badges128 silver badges137 bronze badges
8
You can find the min/max index and value at the same time if you enumerate the items in the list, but perform min/max on the original values of the list. Like so:
import operator
min_index, min_value = min(enumerate(values), key=operator.itemgetter(1))
max_index, max_value = max(enumerate(values), key=operator.itemgetter(1))
This way the list will only be traversed once for min (or max).
jamylak
128k30 gold badges230 silver badges230 bronze badges
answered Mar 19, 2010 at 0:18
Matt AndersonMatt Anderson
19.2k11 gold badges41 silver badges57 bronze badges
2
If you want to find the index of max within a list of numbers (which seems your case), then I suggest you use numpy:
import numpy as np
ind = np.argmax(mylist)
answered Nov 23, 2012 at 17:41
dr.hazdr.haz
1,4571 gold badge10 silver badges5 bronze badges
1
Possibly a simpler solution would be to turn the array of values into an array of value,index-pairs, and take the max/min of that. This would give the largest/smallest index that has the max/min (i.e. pairs are compared by first comparing the first element, and then comparing the second element if the first ones are the same). Note that it’s not necessary to actually create the array, because min/max allow generators as input.
values = [3,4,5]
(m,i) = max((v,i) for i,v in enumerate(values))
print (m,i) #(5, 2)
answered Dec 21, 2012 at 11:53
Ant6nAnt6n
1,8371 gold badge20 silver badges25 bronze badges
0
seq=[1.1412, 4.3453, 5.8709, 0.1314]
seq.index(min(seq))
Will give you first index of minimum.
Asclepius
56.4k17 gold badges164 silver badges142 bronze badges
answered Sep 7, 2013 at 21:30
AndyAndy
4894 silver badges3 bronze badges
1
I was also interested in this and compared some of the suggested solutions using perfplot (a pet project of mine).
It turns out that
min(range(len(a)), key=a.__getitem__)
is the fastest method for small and large lists.
(In former versions, np.argmin
used to take the cake.)
Code for generating the plot:
import numpy as np
import operator
import perfplot
def min_enumerate(a):
return min(enumerate(a), key=lambda x: x[1])[0]
def min_enumerate_itemgetter(a):
min_index, min_value = min(enumerate(a), key=operator.itemgetter(1))
return min_index
def getitem(a):
return min(range(len(a)), key=a.__getitem__)
def np_argmin(a):
return np.argmin(a)
b = perfplot.bench(
setup=lambda n: np.random.rand(n).tolist(),
kernels=[
min_enumerate,
min_enumerate_itemgetter,
getitem,
np_argmin,
],
n_range=[2**k for k in range(15)],
)
b.show()
answered May 23, 2017 at 7:58
Nico SchlömerNico Schlömer
52.6k26 gold badges196 silver badges243 bronze badges
3
I think the best thing to do is convert the list to a numpy array
and use this function :
a = np.array(list)
idx = np.argmax(a)
answered Jan 5, 2019 at 6:24
0
I think the answer above solves your problem but I thought I’d share a method that gives you the minimum and all the indices the minimum appears in.
minval = min(mylist)
ind = [i for i, v in enumerate(mylist) if v == minval]
This passes the list twice but is still quite fast. It is however slightly slower than finding the index of the first encounter of the minimum. So if you need just one of the minima, use Matt Anderson’s solution, if you need them all, use this.
answered Apr 14, 2011 at 18:22
4
After you get the maximum values, try this:
max_val = max(list)
index_max = list.index(max_val)
Much simpler than a lot of options.
answered Jan 11, 2018 at 16:57
alpha_989alpha_989
4,8212 gold badges37 silver badges48 bronze badges
Use a numpy array and the argmax() function
a=np.array([1,2,3])
b=np.argmax(a)
print(b) #2
answered Jan 29, 2018 at 18:07
0
Pandas has now got a much more gentle solution, try it:
df[column].idxmax()
answered Aug 10, 2020 at 9:44
This is possible using the built-in enumerate()
and max()
function and the optional key
argument of the max()
function and a simple lambda expression:
theList = [1, 5, 10]
maxIndex, maxValue = max(enumerate(theList), key=lambda v: v[1])
# => (2, 10)
In the docs for max()
it says that the key
argument expects a function like in the list.sort()
function. Also see the Sorting How To.
It works the same for min()
. Btw it returns the first max/min value.
answered Oct 28, 2016 at 9:35
Simon HänischSimon Hänisch
4,6602 gold badges30 silver badges42 bronze badges
2
Use numpy module’s function numpy.where
import numpy as n
x = n.array((3,3,4,7,4,56,65,1))
For index of minimum value:
idx = n.where(x==x.min())[0]
For index of maximum value:
idx = n.where(x==x.max())[0]
In fact, this function is much more powerful. You can pose all kinds of boolean operations
For index of value between 3 and 60:
idx = n.where((x>3)&(x<60))[0]
idx
array([2, 3, 4, 5])
x[idx]
array([ 4, 7, 4, 56])
answered Apr 16, 2015 at 8:29
Ishan TomarIshan Tomar
1,4681 gold badge16 silver badges20 bronze badges
4
Say you have a list such as:
a = [9,8,7]
The following two methods are pretty compact ways to get a tuple with the minimum element and its index. Both take a similar time to process. I better like the zip method, but that is my taste.
zip method
element, index = min(list(zip(a, range(len(a)))))
min(list(zip(a, range(len(a)))))
(7, 2)
timeit min(list(zip(a, range(len(a)))))
1.36 µs ± 107 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
enumerate method
index, element = min(list(enumerate(a)), key=lambda x:x[1])
min(list(enumerate(a)), key=lambda x:x[1])
(2, 7)
timeit min(list(enumerate(a)), key=lambda x:x[1])
1.45 µs ± 78.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
answered Feb 26, 2018 at 10:29
Pablo MPAPablo MPA
611 silver badge3 bronze badges
Why bother to add indices first and then reverse them? Enumerate() function is just a special case of zip() function usage. Let’s use it in appropiate way:
my_indexed_list = zip(my_list, range(len(my_list)))
min_value, min_index = min(my_indexed_list)
max_value, max_index = max(my_indexed_list)
answered Mar 17, 2015 at 13:45
As long as you know how to use lambda and the «key» argument, a simple solution is:
max_index = max( range( len(my_list) ), key = lambda index : my_list[ index ] )
answered Jan 24, 2017 at 22:41
VeigaVeiga
2121 silver badge9 bronze badges
1
Simple as that :
stuff = [2, 4, 8, 15, 11]
index = stuff.index(max(stuff))
answered Mar 12, 2017 at 19:51
Just a minor addition to what has already been said.
values.index(min(values))
seems to return the smallest index of min. The following gets the largest index:
values.reverse()
(values.index(min(values)) + len(values) - 1) % len(values)
values.reverse()
The last line can be left out if the side effect of reversing in place does not matter.
To iterate through all occurrences
indices = []
i = -1
for _ in range(values.count(min(values))):
i = values[i + 1:].index(min(values)) + i + 1
indices.append(i)
For the sake of brevity. It is probably a better idea to cache min(values), values.count(min)
outside the loop.
answered Apr 6, 2012 at 16:09
1
A simple way for finding the indexes with minimal value in a list if you don’t want to import additional modules:
min_value = min(values)
indexes_with_min_value = [i for i in range(0,len(values)) if values[i] == min_value]
Then choose for example the first one:
choosen = indexes_with_min_value[0]
answered Aug 9, 2016 at 12:17
Assuming you have a following list my_list = [1,2,3,4,5,6,7,8,9,10]
and we know that if we do max(my_list)
it will return 10
and min(my_list)
will return 1
. Now we want to get the index of the maximum or minimum element we can do the following.
my_list = [1,2,3,4,5,6,7,8,9,10]
max_value = max(my_list) # returns 10
max_value_index = my_list.index(max_value) # retuns 9
#to get an index of minimum value
min_value = min(my_list) # returns 1
min_value_index = my_list.index(min_value) # retuns 0
answered Nov 14, 2020 at 12:05
Hadi MirHadi Mir
4,4292 gold badges27 silver badges30 bronze badges
https://docs.python.org/3/library/functions.html#max
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0]
To get more than just the first encountered, use the sort method.
import operator
x = [2, 5, 7, 4, 8, 2, 6, 1, 7, 1, 8, 3, 4, 9, 3, 6, 5, 0, 9, 0]
min = False
max = True
min_val_index = sorted( list(zip(x, range(len(x)))), key = operator.itemgetter(0), reverse = min )
max_val_index = sorted( list(zip(x, range(len(x)))), key = operator.itemgetter(0), reverse = max )
min_val_index[0]
>(0, 17)
max_val_index[0]
>(9, 13)
import ittertools
max_val = max_val_index[0][0]
maxes = [n for n in itertools.takewhile(lambda x: x[0] == max_val, max_val_index)]
answered Sep 17, 2015 at 20:42
The DemzThe Demz
6,9965 gold badges38 silver badges42 bronze badges
What about this:
a=[1,55,2,36,35,34,98,0]
max_index=dict(zip(a,range(len(a))))[max(a)]
It creates a dictionary from the items in a
as keys and their indexes as values, thus dict(zip(a,range(len(a))))[max(a)]
returns the value that corresponds to the key max(a)
which is the index of the maximum in a. I’m a beginner in python so I don’t know about the computational complexity of this solution.
answered Oct 12, 2019 at 9:10
This tutorial is about how to find the Index of the minimum value in the list using python. We are assuming that you’re already familiar with the list, its syntax, and its uses. We also discuss the index of the maximum value in the list.
If you want to learn more about lists in Python, See Python List Tutorials
Given a list of N integers, find the index of elements whose value is minimum among all the elements in the list. There are two cases which are given below:
Case 1: When there is only one minimum value in the list. For example: Input List: 23,56,32,89,21,44,51 Output : The index of minimum value is 5. Case 2: When the minimum value is repeated multiple times in the list. For example: Input List: 32,23,56,32,89,41,32,23,44,51,23 Output : The minimum value occurs at 2nd, 8th and 11th indices.
Let’s discuss the above-mentioned two cases using different appr3oaches along with examples.
Index of minimum value using min() and index() Python functions
The python has a built-in function of min() which returns the minimum value in the list and index() which returns the index of the element. These two functions can be used to find the index of a minimum element in a single line code.
Example 1:
Given a list, find the index of a minimum element.
#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value min_index=list1.index(min_value) return min_index if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Index of minimum value is ",min_list1) min_list2 = get_minvalue(list2) print("Index of minimum value is ",min_list2)
Output:
In the above example, there is only one minimum value in list 1 whose index is returned whereas in list 2, minimum value occurs more than once but in the output, only the index 0 is returned. This is because, in the case of multiple occurrences of an item, the index() function returns the index of only the first occurrence. This approach works well for case 1 but fails to display all the indices in case of multiple occurrences.
Approach 2: using min() and For Loop:
The second approach is based on the combination of min() and for a loop. This approach overcomes the limitation of the previous approach. It is able to return all the indices in case of multiple occurrences of the same elements. First of all, we acquire the minimum element using the min() command. Then iterate over the list using for loop and store the indexes of all those positions whose value is equal to the minimum value. Let’s understand this through an example.
Example:
#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value min_index=[] for i in range(0,len(inputlist)): if min_value == inputlist[i]: min_index.append(i) return min_index if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Index of minimum value is ",min_list1) min_list2 = get_minvalue(list2) print("Index of minimum value is ",min_list2)
Output:
Now, you can see that the code has returned all the indices of the minimum value in the list2. In the above example, you can also use enumerate function in place of the range function. The results would be the same. This code can be further modified by using list comprehension which in turn reduces the length of code. Below is the implementation of the above code using list comprehension.
Example:
#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value res = [i for i,val in enumerate(inputlist) if val==min_value] return res if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Indices of minimum value: ",min_list1) min_list2 = get_minvalue(list2) print("Indices of minimum value: ",min_list2)
Output:
The above code uses list comprehension and returns the same results as in the previous example.
This tutorial discusses two different approaches to find the indices of minimum values in the list along with their limitations. If you have any questions regarding this tutorial, let us know in the comments. It would be highly appreciated.
See more Python Tutorials