How do you find the minimum of a list of numbers?

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.

Python3




# 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

Python3




# 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.

Python3




# 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.

Python3




# 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




Article Tags :

Python

Python Programs

Python list-programs

python-list

Practice Tags :

python-list

Read Full Article

Python List min[] Method

Advertisements


Previous Page

Next Page

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!

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.

Find Smallest Number in Python List

You can find the smallest number of a list in Python using min[] function, sort[] function or for loop.

We shall look into the following processes to find the smallest number in a list, with examples.

  • Use min[] builtin function
  • Use List sort[] function
  • Use Python For Loop

Choose one, based on your program requirements or your own personal performance recommendations.

Example 1: Find the smallest number using min[]

min[] function can take a list as argument and return the minimum of the elements in the list.

In this example, we will take a list of numbers and find the smallest of the list using min[] function.

Python Program

# python program to find the smallest number # list of numbers a = [18, 52, 23, 41, 32] # find smallest number using min[] function smallest = min[a] # print the smallest number print[f'Smallest number in the list is : {smallest}.']Run

Output

Smallest number in the list is : 18.

Example 2: Find the smallest number using sort[] function

We know that sort[] function sorts a list in ascending or descending order. After you sort a list, you have your smallest number at the start of the list if you have sorted in ascending order or at the end of the list if you have sorted in descending order.

Python Program

# python program to find the smallest number # list a = [18, 52, 23, 41, 32] # sort the list, default is in ascending order a.sort[] # smallest number smallest = a[0] # print the smallest number print["Smallest number is: ",smallest]Run

Output

Smallest number is: 18

Example 3: Find the smallest number using for loop

Though finding the smallest number using sort[] function is easy, using Python For Loop does it relatively faster with less number of operations. And also, we do not change the order of elements in the given list.

Python Program

a=[18, 52, 23, 41, 32] # smallest number smallest = a[0] if a else None # find smallest for i in a: if i

Bài mới nhất

Chủ Đề