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.
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.
Syntax
min(List)
Code example
numbers = [30, 50, 90, 10, 70, 20,]
result_min = min(numbers)
print("Minimum Value:", result_min)
Output
Minimum Value: 10
- The code creates a list of numbers called 'numbers'.
- The code finds the minimum value in the list 'numbers' using the min() function.
- 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))
Output
Minimum value is: 10
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.
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)
Output
Minimum value is: 10
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.
- Find all indexes of an item in a list using python
- Find the smallest number in a python list
- Find the largest number in a list Python
- Find the most repeated item from a list in Python
- Get last item or value from a List in python
- Remove items less than a specific value from Python list
- Get the largest value from a List in Python