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.
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)) |
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)) |
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)
Please Login to comment...