Python Program to Find Smallest Number in List

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.

Python Find Smallest Number in List

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

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

Examples

1. Find the smallest number in given list 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 Code Copy

Output

Smallest number in the list is : 18.

2. Find the smallest number in given list 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 Code Copy

Output

Smallest number is: 18

3. Find the smallest number in given list 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<smallest:
		smallest=i

print("Smallest element is: ", smallest)
Run Code Copy

Output

Smallest element is: 18

In this example,

  1. We have assumed a list and initialized largest number variable to first element of the list. If the list is empty, smallest number will be None.
  2. Then we iterated over the list using for loop.
    • During each iteration, we checked if the smallest number is greater than the element. If that is the case, we assigned our smallest number with the element.
  3. When you complete traversing the list, you will end up with the smallest number of the list in your variable.

Summary

In this tutorial of Python Examples, we learned some of the ways to find the smallest number in a Python list with the help of well detailed Python example programs.

Related Tutorials

Code copied to clipboard successfully 👍