Python – Find the index of Minimum element in list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum element indices in list
# Using list comprehension + min() + enumerate()
  
# initializing list
test_list = [2, 5, 6, 2, 3, 2]
  
# printing list
print("The original list : " + str(test_list))
  
# Minimum element indices in list
# Using list comprehension + min() + enumerate()
temp = min(test_list)
res = [i for i, j in enumerate(test_list) if j == temp]
  
# Printing result
print("The Positions of minimum element : " + str(res))

chevron_right


Output :



The original list : [2, 5, 6, 2, 3, 2]
The Positions of minimum element : [0, 3, 5]

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum element indices in list
# Using loop + min()
  
# initializing list
test_list = [2, 5, 6, 2, 3, 2]
  
# printing list
print("The original list : " + str(test_list))
  
# Minimum element indices in list
# Using loop + min()
temp = min(test_list)
res = []
for idx in range(0, len(test_list)):
    if temp == test_list[idx]:
        res.append(idx)
  
# Printing result
print("The Positions of minimum element : " + str(res))

chevron_right


Output :

The original list : [2, 5, 6, 2, 3, 2]
The Positions of minimum element : [0, 3, 5]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.