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 in?
- How to Find Index of Small Element in a List in Case of Multiple Occurrences in Python?
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:
Print the list elements by calling the print statement:
Now, set the index value:
Get the length of the created list through the “len()” method and store into the “listLength” variable:
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:
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:
Call the “array()” method with the created list as an argument and pass to the “array”:
Now, use the “argmin()” method without argument:
Invoke the “print()” function to get the result:
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:
Next, create an empty list:
Call the “len()” method to get the length of the list:
After that, create a sequence of numbers from “0” to the list length by utilizing the “range()” method:
Use the “for” loop to iterate over the sequence of list numbers while executing the loop:
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.