We use lists in a python program to store different types of objects when we need random access. In this article, we will discuss different ways to find the index of the minimum element in a list in python.
Index of Minimum Element in a List Using for Loop
We use a for loop in python to iterate over the elements of a container object like a list. To find the index of minimum element in a list using a for loop in python, we can use the len()
function and the range()
function.
The len() Function in Python
The len()
function in python is used to find the length of a collection object like a list or tuple. It takes the container object like a list as its input argument and returns the length of the collection object after execution. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
print("The list is:", myList)
list_len = len(myList)
print("Length of the list is:", list_len)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
Length of the list is: 11
Here, we have passed a list with 11 elements to the len()
function. After execution, it returns the same value.
The range() Function in Python
The range()
function is used to generate a sequence of numbers in python. In the simplest case, the range()
function takes a positive number N as an input argument and returns a sequence of numbers containing numbers from 0 to N-1. You can observe this in the following example.
sequence = range(11)
print("The sequence is:", sequence)
Output:
The sequence is: range(0, 11)
To find the index of minimum element in a list in python using the for loop, len()
function, and the range()
function, we will use the following steps.
- First, we will calculate the length of the input list using the
len()
function. We will store the value in a variablelist_len
. - After calculating the length of the list, we will create a sequence of numbers from 0 to
list_len-1
using therange()
function. - Now, we will define a variable
min_index
and assign it the value 0 assuming that the minimum element of the list is present at index 0. - After creating the sequence, we will iterate over the numbers in the sequence using a for loop. While iteration, we will access the elements in the input list at each index.
- At each index, we will check if the element at the current index is less than the element at the
min_index
index in the list. If the current element is less than the element atmin_index
, we will updatemin_index
to the current index.
After execution of the for loop, you will get the index of the minimum element in the list in the min_index
variable. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_index = 0
list_len = len(myList)
for index in range(list_len):
if myList[index] < myList[min_index]:
min_index = index
print("Index of the minimum element is:", min_index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Index of the minimum element is: 3
In the above approach, you will get the leftmost index of the minimum element if there are multiple occurrences of the element in the list. To obtain the rightmost index at which the minimum element is present, you can use the less than or equal to operator instead of less than operator while comparing elements of the list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_index = 0
list_len = len(myList)
for index in range(list_len):
if myList[index] <= myList[min_index]:
min_index = index
print("Index of the minimum element is:", min_index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Index of the minimum element is: 11
Index of Minimum Element in a List Using the min() Function and index() Method
Instead of iterating the entire list using for loop, we can use the min()
function and the index()
method to find the index of the minimum element in a list in python.
The min() Function in Python
The min()
function is used to find the minimum element in a container object like a list, tuple, or set. The min()
function takes a collection object like a list as its input argument. After execution, it returns the minimum element in the list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_val = min(myList)
print("The minimum value is:", min_val)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
The minimum value is: 1
The index() Method in Python
The index()
method is used to find the position of an element in a list. When invoked on a list, the index()
method takes an element as its input argument. After execution, it returns the index of the first occurrence of the element. For instance, we can find the index of element 23 in the list as shown below.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
index = myList.index(23)
print("Index of 23 is:", index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Index of 23 is: 2
As said above, if there are multiple occurrences of a single element, the index()
method only returns the leftmost index of the element. If we would have passed element 1 as the input argument to the index()
method, the output would have been 3 despite the fact that element 1 is also present at the index 11.
If the element given in the input argument is not present in the list, the index()
method raises a ValueError
exception saying that the element is not present in the list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
index = myList.index(105)
print("Index of 105 is:", index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
index = myList.index(105)
ValueError: 105 is not in list
Here, we have passed 105 as an input argument to the index()
method. However, 105 is not present in the list. Therefore, the program runs into the ValueError
exception showing that 105 is not present in the list.
To find the index of the minimum element in a list using the min()
function and the index()
function, we will use the following steps.
First, we will find the minimum element in the list using the min()
function. We will store the value in a variable min_val
.
After finding the minimum value in the list, we will invoke the index()
method on the list with min_val
as its input argument. After execution, the index()
method will return the index of the minimum element in the list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_val = min(myList)
index = myList.index(min_val)
print("Index of minimum value is:", index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Index of minimum value is: 3
This approach can only find the leftmost index of the minimum element in a list in case of multiple occurrences of the element. If you want to obtain the rightmost index of the minimum element, you can use the approach with for loop and the len()
function discussed in the previous section.
Index of Minimum Element in a List Using Numpy Module
The numpy
module has been designed to manipulate numbers and arrays in an efficient manner. We can also find the index of the minimum element in a list in python using the numpy
module.
The numpy
module provides us with the argmin()
method to find the index of the minimum element in a NumPy array. The argmin()
method, when invoked on a numpy
array, returns the index of the minimum element in the array.
To obtain the index of the minimum element in a list using the argmin()
method, we will first convert the list into a numpy array. For this, we will use the array()
function.
The array()
function takes a list as its input argument and returns a numpy array. You can observe this in the following example.
import numpy
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
array = numpy.array(myList)
print("The array is:", array)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
The array is: [11 2 23 1 32 12 44 34 55 46 21 1 12]
After converting the list to a numpy array, we will invoke the argmin()
method on the array. After execution, the argmin()
method will return the index of the minimum element.
import numpy
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
array = numpy.array(myList)
min_index = array.argmin()
print("Index of minimum element is:", min_index)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Index of minimum element is: 3
When there are multiple occurrences of the minimum element, the argmin()
function will return the leftmost index of the minimum element. In the above example, you can see that the argmin()
method returns 3 even if the minimum element 1 is also present at index 11.
Index of the Minimum Element in a List in Case of Multiple Occurrences
Sometimes, we might need to find all the occurrences of the minimum element in a list. In the following sections, we will discuss different ways to find the index of the minimum element in a list in case of multiple occurrences.
Index of Minimum Element in a List Using the min() Function and For Loop
When there are multiple occurrences of the minimum element in the list, we can use the min()
function and the for loop to obtain the indices of the minimum element. For this, we will use the following steps.
- First, we will create an empty list named
min_indices
to store the indices of the minimum element. - Then, we will find the length of the input list using the
len()
function. We will store the length in a variablelist_len
. - After obtaining the length of the list, we will create a sequence of numbers from 0 to list_len-1 using the
range()
function. - Next, we will find the minimum element in the list using the
min()
function. - After finding the minimum element, we will iterate over the sequence of numbers using a for loop. While iteration, we will check if the element at the current index in the list is equal to the minimum element.
- If we find an element that is equal to the minimum element, we will append its index to the
min_indices
list using theappend()
method. Theappend()
method, when invoked on themin_indices
list, will take theindex
as its input argument. After execution, it will add theindex
to themin_indices
list as an element. - If the number at the current index is not equal to the minimum element, we will move to the next element.
- If we find an element that is equal to the minimum element, we will append its index to the
After execution of the for loop, we will get the indices of the minimum element in the list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_val = min(myList)
list_indices = []
list_len = len(myList)
sequence = range(list_len)
for index in sequence:
if myList[index] == min_val:
list_indices.append(index)
print("Indices of minimum element are:", list_indices)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Indices of minimum element are: [3, 11]
Index of Minimum Element in a List Using the min() Function and List Comprehension
List comprehension is used to create a new list using the elements of an existing list by imposing some conditions on the elements. The general syntax of list comprehension is as follows.
new_list=[f(element) for element in existing_list if condition]
Here,
new_list
is the list created after the execution of the statement.existing_list
is the input list.- The
element
represents one of the elements in the existing list. This variable is used to iterate over theexisting_list
. condition
is a condition imposed on the element orf(element)
.
To find the indices of the minimum element in the list using list comprehension in python, we will use the following steps.
- First, we will find the length of the input list using the
len()
function. We will store the length in a variablelist_len
. - After obtaining the length of the list, we will create a sequence of numbers from 0 to list_len-1 using the
range()
function. - Next, we will find the minimum element in the list using the
min()
function. - After finding the minimum element, we will use list comprehension to obtain the list of indices of the minimum element.
- In list comprehension,
- We will use the sequence of numbers in place of the
existing_list
. - The
element
will represent the elements of the sequence of numbers. f(element)
will be equal to theelement
.- In place of the
condition
, we will use the condition that the value in the input list at the index element should be equal to the minimum element.
- We will use the sequence of numbers in place of the
After execution of the statement, we will get new_list
containing the indices of the minimum element in the list as shown below.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
min_val = min(myList)
list_len = len(myList)
sequence = range(list_len)
new_list = [index for index in sequence if myList[index] == min_val]
print("Indices of minimum element are:", new_list)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Indices of minimum element are: [3, 11]
Index of Minimum Element in a List Using min() Function and enumerate() Function
Instead of using the len()
function and the range()
function, we can use the enumerate()
function with the min()
function to find the indices of minimum elements in a list.
The enumerate() Function in Python
The enumerate()
function is used to enumerate the elements of a container object such as a list or tuple.
The enumerate()
function takes a container object like a list as its input argument. After execution, it returns an enumerated list containing tuples. Each tuple in the list contains two elements. The first element of the tuple is the index that is assigned to an element. The second element is the corresponding element from the original list that is given as input to the enumerate()
function. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
print("Enumerated list is:", enumerated_list)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Enumerated list is: [(0, 11), (1, 2), (2, 23), (3, 1), (4, 32), (5, 12), (6, 44), (7, 34), (8, 55), (9, 46), (10, 21), (11, 1), (12, 12)]
To find the indices of minimum elements in the list using the enumerate()
function, we will use the following steps.
- First, we will create an empty list named
min_indices
to store the indices of the minimum element. - After that, we will find the minimum element in the input list using the
min()
function. - Then, we will create the enumerated list from the input list using the
enumerate()
function. - After obtaining the enumerated list, we will iterate over the tuples in the enumerated list using a for loop.
- While iterating the tuples, we will check if the element in the current tuple is equal to the minimum element.
- If the element in the current tuple is equal to the minimum element, we will append the current index to
min_indices
using theappend()
method. Otherwise, we will move to the next tuple in the enumerated list.
After execution of the for loop, we will get the indices of the minimum element in the min_indices
list. You can observe this in the following example.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
min_indices = []
min_element = min(myList)
for element_tuple in enumerated_list:
index = element_tuple[0]
element = element_tuple[1]
if element == min_element:
min_indices.append(index)
print("Indices of minimum element are:", min_indices)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Indices of minimum element are: [3, 11]
Instead of using a for loop to iterate over the tuples in the enumerated list, we can use list comprehension to find the indices of the minimum element in the list as shown below.
myList = [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
min_element = min(myList)
min_indices = [index for (index, element) in enumerated_list if element == min_element]
print("Indices of minimum element are:", min_indices)
Output:
The list is: [11, 2, 23, 1, 32, 12, 44, 34, 55, 46, 21, 1, 12]
Indices of minimum element are: [3, 11]
Conclusion
In this article, we have discussed different ways to find the index of the minimum element in a list in python. We also discussed finding all the indices of the minimum element in the list in case of multiple occurrences of the minimum element.
To find the index of the minimum element in a list in python, I would suggest you use the min()
function with the index()
method. This approach gives the leftmost index of the minimum element in case of multiple occurrences.
If you need to find the rightmost index of the minimum element in a list in python, you can use the approach using the for loop and less than or equal to operator.
In case of multiple occurrences, if you need to obtain all the indices of the minimum element in a list in python, you can either use the approach with for loop or the approach with the enumerate()
function. Both have almost the same execution efficiency.
I hope you enjoyed reading this article. To learn more about python programming, you can read this article on how to remove all occurrences of a character in a list in Python. You might also like this article on how to check if a python string contains a number.
The post Index of Minimum Element in a List in Python appeared first on PythonForBeginners.com.