Python min value in list

Find the Index of Minimum value in the list of Python

While working with lists, you often encounter situations where you need to find specific values or perform statistical analysis based on these values. This could involve tasks such as identifying the minimum or maximum value in a list, determining the frequency of certain elements, calculating various statistical measures, etc. One of the common tasks among them is to find an index of the minimum value in the list using Python. Assuming that you’re already familiar with the list, its syntax, and its uses, we will discuss different methods by which we can access the index of the minimum value in the list.

Using the min() function along with the index() function, we can find the minimum value and then its index from the list. Another approach is to use min() function along with the for loop. In this tutorial, we will discuss these methods in detail.

If you want to learn more about lists in Python, See Python List Tutorials

Читайте также:  HTML JS REPLACE

Given a list of n integers, find the index of elements whose value is minimum among all the elements in the list. We will discuss the following two cases:

Case 1: When there is only one minimum value in the list. For example: Input List: 23,56,32,89,21,44,51 Output : The index of minimum value is 5. Case 2: When the minimum value is repeated multiple times in the list. For example: Input List: 32,23,56,32,89,41,32,23,44,51,23 Output : The minimum value occurs at 2nd, 8th and 11th indices.

Let’s discuss the above-mentioned two cases using different approaches along with examples.

Get the Index of minimum value using min() and index() functions

The python has a built-in function of min() which returns the minimum value in the list. Then, pass this minimum value to the index() function which returns the index of the element. These two functions are used in combination to find the index of a minimum element in a single line code.

Given a list, find the index of a minimum element.

#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value min_index=list1.index(min_value) return min_index if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Index of minimum value is ",min_list1) min_list2 = get_minvalue(list2) print("Index of minimum value is ",min_list2)
Index of minimum value is 4 Index of minimum value is 0

In the above example, there is only one minimum value in list1 whose index is returned. However, in list2, minimum value occurs more than once. But, the program returns index 0 only in the output. This is because, in the case of multiple occurrences of an item, the index() function returns the index of only the first occurrence. This approach works well for case 1 but fails to display all the indices in case of multiple occurrences.

Читайте также:  Hands on design patterns with kotlin

Using Min() function with For loop to access the index with minimum value

Another way is to use a combination of min() and for loop. This method has the advantage of being able to handle cases where there are multiple occurrences of the minimum value in the list. It returns all the indices associated with the minimum value in case of multiple occurrences of the same elements. Let’s see how it works.

First of all, get the minimum element using the min() function. Then, iterate over the list using for loop and store the indexes of all those positions whose value is equal to the minimum value. Let’s understand this through an example.

#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value min_index=[] for i in range(0,len(inputlist)): if min_value == inputlist[i]: min_index.append(i) return min_index if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Index of minimum value is ",min_list1) min_list2 = get_minvalue(list2) print("Index of minimum value is ",min_list2)
Index of minimum value is [4] Index of minimum value is [1, 7, 10]

Now, the code has returned all the indices of the minimum value in the list2. In the above example, you can also use enumerate function in place of the range function. The results would be the same.

Using List Comprehension

This code can be further modified by using list comprehension which in turn reduces the length of code. Below is the implementation of the above code using list comprehension.

#function which returns the index of minimum value in the list def get_minvalue(inputlist): #get the minimum value in the list min_value = min(inputlist) #return the index of minimum value res = [i for i,val in enumerate(inputlist) if val==min_value] return res if __name__ == "__main__" : #create and initialize a list list1 = [23,56,32,89,21,44,51] list2 = [32,23,56,32,89,41,32,23,44,51,23] min_list1 = get_minvalue(list1) print("Indices of minimum value: ",min_list1) min_list2 = get_minvalue(list2) print("Indices of minimum value: ",min_list2)
Index of minimum value is [4] Index of minimum value is [1, 7, 10]

The above code uses list comprehension and returns the same results as in the previous example.

Finding the index of a minimum value in a list might provide useful insights and aid in decision-making or problem-solving. The most common example is a route optimization problem where you have a list of distances or costs associated with different routes. Businesses can improve their logistics and transportation processes by identifying the index of minimal value. This value can then be used to determine the most efficient or economical route.

In this tutorial, we have discussed two different approaches to finding the indices of minimum values in the list along with their limitations. If you have any questions regarding this tutorial, let us know in the comments. Your feedback would be highly appreciated.

Источник

Find Max And Min In A List Python

Working with lists is very common in Python and even more common is to find max and min in a list Python. We will see 3 different methods for each to find min and max in a python list.

A list in python is a collection of user-stored data in order. In a list, data can be of any type like string, integer, float, boolean, etc.

A list can be created by using square brackets [ ] . Example [1, «a», True]

A list can have mixed data or can have only one data type. To find max and min in a list, we will work on a list of numbers. A list that has only integers or float values both negative and positive can be used to find max and min.

find max and min in a list python

Find Maximum Value In List

To find maximum value in a list we will use 3 different methods.

  1. Using max() function
  2. Finding the maximum value using for loop
  3. Using sort() function

1. Using max() function

The max() function is built-in function in Python. It returns the maximum value in a list. It takes a list as an argument and returns the maximum value in the list.

The function accepts the list as an argument. Here is the syntax:

Let’s see an example to find the maximum value in a list.

num = [4, 6, 1, 3, 9, 2] # Find the maximum value in the list print(max(num))

The max() function can also be used to find the maximum value in a list of strings.

To compare the values among strings, the max() function uses their ASCII values. For example, the ASCII value of a is 97 and the ASCII value of z is 122.

str = ["a", "b", "c", "d", "e"] print(max(str))

In the above example, max of the list is e because the ASCII value of e is 101 which is the highest in the list.

Note : max() function does not work on lists of mixed data types.

2. Finding the maximum value using for loop

You can create your own Python function to find the maximum value in a list using for loop and if condition.

Algorithm to find the maximum value in a list

  1. Create a variable max and assign it to the first element of the list
  2. Create a for loop to iterate over the list
  3. Check if the current element is greater than max , if yes then assign it to max . Now current element will become the new max .
  4. Keep iterating over the list until the end of the list and return max .

The example below is implemented using the above algorithm.

def max_value(list): # set first element as max max = list[0] for i in list: # check if the current element is greater than max if i > max: max = i return max num = [12, 65, 54, 39, 102, 37, 72, 33, 5, -28, 0, 15] print(max_value(num))

The above example will find the maximum value in the list and print it.

3. Using sort() function: find max

The sort() function is another built-in function in python using which we can find the maximum value in a list. The sort() function sorts the list in ascending order, which means smallest stays at the first position and largest stays at the last position.

The sort() function takes a list as an argument. TO get the maximum value in a list, you can sort the list and picks the last element of the list.

num = [12, 65, 54, 39, 102, 37, 72, 33, 5, -28, 0, 15] # sort the list num.sort() max = num[-1] print(max)

The above example sorts the list and then picks the last element from the sorted list which is the maximum value in the list.

Find Minimum Value In List

Again to find the minimum from a list we can use similar 3 methods but this time just to find the minimum value.

  1. Using min() function
  2. Finding the minimum value using for loop
  3. Using sort() function

1. Using min() function

The min() function in python finds the minimum value from a list and returns it. It can work with both numbers and strings as well but not with mixed data types.

In the following example, the min() function finds the minimum value in a list of numbers and prints the output.

num = [4, 6.4, 1, -3, 0, 2.5] # Find the minimum value in the list print(min(num))

The min() function can also find the minimum value in a list of strings by comparing their ASCII values.

2. Finding the minimum value using for loop

Creating own function to find minimum value in a list by comparing one value to each other.

Algorithm to find the minimum value in a list

  1. Store the first element of the list in a variable min
  2. Now loop through the list element and compare elements from each other. If the current element is less than min , then assign it to min . Now you have the new min value.
  3. Keep repeating the above steps until the end of the list. At the last min variable will have the actual minimum value of the string.
  4. Return min .

Here is the implementation of the above algorithm.

def min_value(list): # set first element as min min = list[0] for i in list: # check if the current element is less than min if i < min: min = i return min num = [12, 65, 54, 39, 102, 37, 72, 33, 5, -28, 0, 15] print(min_value(num))

The function will find the minimum value in the list and returns it as output.

3. Using sort() function : find min

We can again use the sort() function here to sort the elements of a list in ascending order and then picks the first element of the list which is the minimum value.

num = [12, 65, 54, 39, 102, 37, 72, 33, 5, -28, 0, 15] min = num[0] print(min)

The sort() method sorted the element in ascending order which puts the smallest value in the list at the first position which is -28.

Conclusions

In this short guide, we have covered 3 methods for each to find max and min in a list python. We have also covered the min(), max() and sort() as in-built functions in python and also created a custom function to find minimum and maximum.

Источник

Оцените статью