Contents
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<smallest:
smallest=i
print("Smallest element is: ", smallest)
Run Output
Smallest element is: 18
In this example,
- 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.
- 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.
- 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
- Python List of Lists
- Python – How to Create an Empty List?
- Python – Check if Element is in List
- How to Sort Python List?
- Python List of Dictionaries
- Python – Check if List Contains all Elements of Another List
- Python List without Last Element
- How to Append List to Another List in Python? – list.extend(list)
- Python – Get Index or Position of Item in List
- Python List – Add Item