How do you find the minimum value in a list Python?

Python List min() Method


Advertisements

Previous Page
Next Page

Python program to find smallest number in a list

Given a list of numbers, the task is to write a Python program to find the smallest number in given list.
Examples:

Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output : 1

Method 1 : Sort the list in ascending order and print the first element in the list.




# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", *list1[:1])

Output:

smallest element is: 4

Method 2 : Using min() method




# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 1, 45, 99]
# printing the maximum element
print("Smallest element is:", min(list1))

Output:



Smallest element is: 1

Method 3 : Find min list element on inputs provided by user.




# Python program to find smallest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele= int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Smallest element is:", min(list1))

Output:

Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 11 Enter elements: 99 Smallest element is: 11

Method 4: Find the smallest element in list.




# Python program to find smallest
# number in a list
l=[ int(l) for l in input("List:").split(",")]
print("The list is ",l)
# Assign first element as a minimum.
min1 = l[0]
for i in range(len(l)):
# If the other element is min than first element
if l[i] < min1:
min1 = l[i] #It will change
print("The smallest element in the list is ",min1)

Input:

List: 23,-1,45,22.6,78,100,-5

Output:

The list is ['23', '-1', '45', '22.6', '78', '100','-5'] The smallest element in the list is -5

How do you find the minimum value in a list Python?




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list

Python min()

In this tutorial, we will learn about the Python min() function with the help of examples.

The min() function returns the smallest item in an iterable. It can also be used to find the smallest item between two or more parameters.

Example

numbers = [9, 34, 11, -4, 27]
# find the smallest number min_number = min(numbers)
print(min_number) # Output: -4

The min() function has two forms:

# to find the smallest item in an iterable min(iterable, *iterables, key, default) # to find the smallest item between two or more objects min(arg1, arg2, *args, key)

How to find max and min from a list

Finding the maximum and minimum values in a list is pretty easy because of the built-in max and min functions provided by Python. However, you may be wondering what the logic behind these functions is. Let me explain it with a couple of code snippets.

# Pass a list to this function to check for maximum number def max_check(x): max_val = x[0] for check in x: if check > max_val: max_val = check return max_val
Function to find maximum number of a list.
# Pass a list to this function to check for minimum number def min_check(x): min_val = x[0] for check in x: if check < min_val: min_val = check return min_val
Function to find minimum number of a list.

To validate our above defined functions, let’s pass a list to them.

# Pass a list to this function to check for maximum number def max_check(x): max_val = x[0] for check in x: if check > max_val: max_val = check return max_val # Pass a list to this function to check for minimum number def min_check(x): min_val = x[0] for check in x: if check < min_val: min_val = check return min_val #List my_list = [2, 6, 8, 14, 3, 77, 63] #Printing Values print("Maximum of the list", max_check(my_list)) print("Minimum of the list", min_check(my_list))
Run
Validate the max and min functions.

Note: The functions will generate an error for empty lists.

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

How do you find the minimum value in a list Python?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.