Python minimum value in list

Find minimum value from a List in Python

In this tutorial, we will learn how to find the minimum value from a list in Python. We will take a list of numbers as input from the user and find the minimum value from the list using the min() function.

In Python, there are a number of ways to find the minimum value from a list. One way is to use the built-in min() function. Another way is to use a for loop to iterate through the list, keeping track of the minimum value seen so far.

Which method is best depends on the situation. If the list is very large, or if the values in the list are complex objects, the min() function will be more efficient. If the list is small, or if you need to know the index of the minimum value (in addition to the value itself), a For loop may be more appropriate.

Читайте также:  Интегрированная среда программирования javascript

We will explain all the methods to find the smallest values in a Python List one by one.

Solution 1: Find the minimum value from the list using min() function

Python has a build-in function called min() which can be used to find the minimum value from a list. The syntax for this function is min(list). This function will return the minimum value from the list.

numbers = [30, 50, 90, 10, 70, 20,] result_min = min(numbers) print("Minimum Value:", result_min)
  1. The code creates a list of numbers called ‘numbers‘.
  2. The code finds the minimum value in the list ‘numbers‘ using the min() function.
  3. The code prints the minimum value.

Solution 2: Find the minimum value from a List using Python For loop

If you need to find the minimum value from a list in Python, you can do so using a For loop. This loop will iterate through the list, checking each value to see if it is less than the current minimum value. If it is, the minimum value will be updated. At the end of the loop, the minimum value will be the smallest value in the list.

numbers = [30, 50, 90, 10, 70, 20,] min_val = numbers[0] for num in numbers: if num < min_val: min_val = num print("Minimum value is: <>".format(min_val))

The code is looping through the list of numbers and comparing each value to the min_val variable. If the value of the current number is less than the min_val, the min_val variable is updated with the new value. This process repeats until the end of the list, at which point the minimum value is printed.

Читайте также:  Best time to buy and sell stock java

Solution 3: Using List.sort() function

If you want to find the minimum value from a list in Python, you can use the List.sort() function. This function will sort the list in ascending order, so the first element in the list will be the minimum value.

numbers = [30, 50, 90, 10, 70, 20,] numbers.sort() min_val = numbers[0] print("Minimum value is: ", min_val)

The above code is finding the minimum value in a list of numbers. The list is sorted in ascending order and then the first value in the list is printed, which is the minimum value.

Источник

Python – Find Min Value and its index in List

In this tutorial, we will look at how to find the min value in a Python list and its corresponding index with the help of some examples.

How to get the minimum value in a list in Python?

A simple approach is to iterate through the list and keep track of the minimum value. Alternatively, you can also use the Python built-in min() function to find the minimum value in a list.

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

minimum value in a list

Let’s look at some of the different ways of finding the smallest value and its index in a list.

Loop through the list to find the minimum

Iterate through the list values and keep track of the min value. Here’s an example.

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] for val in ls: if val < min_val: min_val = val # display the min value print(min_val)

Here, we iterate over each value in the list ls and keep track of the minimum value encountered in the variable min_val . After the loop finishes, the variable min_val stores the minimum value present in the list, 1.

You can use this method to get the index corresponding to the minimum value in the list as well. Use an additional variable to keep track of the current minimum value’s index.

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] min_val_idx = 0 for i in range(len(ls)): if ls[i] < min_val: min_val = ls[i] min_val_idx = i # display the min value print(min_val) # display its index print(min_val_idx)

We get the minimum value and its index after the loop finishes. Here we iterate through the list via its index rather than the values. You can also use the enumerate() function to iterate through the index and value together.

Using min() to get the maximum value

You can also use the Python built-in min() function to get the min value in a list. The function returns the minimum value in the passed iterable (for example, list, tuple, etc.).

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min(ls)

Using the min() function is simple and is just a single line code compared to the previous example.

You can use the list index() function to find the index corresponding to the minimum value (assuming you already know the minimum value).

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min_val = min(ls) # display the min value print(min_val) # display its index print(ls.index(min_val))

We get the min value and its index in the list ls .

Note that the list index() function returns the index of the first occurrence of the passed value. If the min value occurs more than once in the list, you’ll only get the index of its first occurrence. You can use list comprehension to get all the indices of occurrence of the min value in the list.

# create a list ls = [3, 6, 1, 2, 1, 5] # find min value min_val = min(ls) print(min_val) # find all indices corresponding to min val min_val_idx = [i for i in range(len(ls)) if ls[i]==min_val] print(min_val_idx)

We get all the indices where the minimum value occurs in the list ls .

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

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.

Источник

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