How to find Smallest Number of a List in Python?


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}.')

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)

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)

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.