Open In App
Related Articles

Python – Find the index of Minimum element in list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# 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))

                    
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

# 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))

                    
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
 
# initializing list
test_list = [2, 5, 6, 2, 3, 2]
 
# printing list
print("The original list : " + str(test_list))
 
# Using numpy to find the indices of minimum element
res = np.where(test_list == np.min(test_list))[0]
 
# Printing result
print("The Positions of minimum element : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

                    

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

# Python3 code to demonstrate working of
# Minimum element indices in list
# Using dictionary
 
# 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 dictionary
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]
 
# Printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads