| by Arround The Web | No comments

Python Find Index of Minimum in List

In Python, a list is an arrangement of bytes along with a dynamically arranged series of items. These items can be any value or component within a list. Additionally, the Python list contains the data objects of each data type and is created through the “[ ]” brackets. Moreover, if users want to get the index value of each list element, they can utilize the Python built-in functions and methods.

This post will elaborate on:

How to Find Index of Small Element in Python List in Case of Single Occurrences?

Sometimes you may have a list that contains multiple elements with different maximum and minimum values. In such a situation, getting the list index of the element which contains the minimum value is required. For this purpose, different functions and libraries are used.

Example 1: Using Loop

First, create and initialize a list with multiple values:

new_list = [90, 65, 10, 3, 5, 12, 76, 1, 55]

 

Print the list elements by calling the print statement:

print("My List Contains:", new_list)

 

Now, set the index value:

minimum_index = 0

 

Get the length of the created list through the “len()” method and store into the “listLength” variable:

listLength = len(new_list)

 

Now, use the for loop with the “if” statement to check the list element index one by one and get the index number which has the minimum value. Call the “print()” function to get it:

for index in range(listLength):
    if new_list[index] < new_list[minimum_index]:
        minimum_index = index
print("Minimum Elements' Index in the List is:", minimum_index)

 

Output

Example 2: Using “numpy” Module

The “numpy” module is also utilized for getting the minimum element index of the list. To do so, initially import the “numpy” module:

import numpy

 

Call the “array()” method with the created list as an argument and pass to the “array”:

array = numpy.array(new_list)

 

Now, use the “argmin()” method without argument:

minimum_index = array.argmin()

 

Invoke the “print()” function to get the result:

print("Minimum Elements' Index in the List is:", minimum_index)

 

Output

How to Find Index of Small Element in Python List in Case of Multiple Occurrences?

To get the index of the lists’ minimum element in case of multiple presences of it in Python, the “min()” function can be used. Additionally, the “for” loop is utilized to get the indices of the minimum element.

Example: Using “min()” Function and “for” Loop

Call the “min()” function with the previously created list as parameter:

minimum_val = min(new_list)

 

Next, create an empty list:

list_ind = []

 

Call the “len()” method to get the length of the list:

listLength = len(new_list)

 

After that, create a sequence of numbers from “0” to the list length by utilizing the “range()” method:

sequence = range(listLength)

 

Use the “for” loop to iterate over the sequence of list numbers while executing the loop:

for index in sequence:
    if new_list[index] == minimum_val:
        list_ind.append(index)
print("Minimum Element Indices in the List are:", list_ind)

 

Here:

  • We will check through the “if” condition the element at the current list index is equal to the minimum element.
  • If the condition is evaluated as true, then we use the “append()” method to append its index to the “index”.
  • If not, then it will be eliminated from the given list.
  • Print the minimum element index of the list.

Output

That’s all! We have elaborated on the methods of finding an index of minimum list elements in Python.

Conclusion

To get the single minimum element index from the list in Python, the iterative function “for” loop and “numpy” module can be used. On the other hand, the “min()” and “for” iterative functions can be utilized for getting the index of multiple minimum elements of the list in Python. This post illustrated the ways to get an index of the minimum list element.

Share Button

Source: linuxhint.com

Leave a Reply