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.
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.
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}.')
Explanation
- A list
a
is initialized with the elements[18, 52, 23, 41, 32]
. - The
min()
function is used to find the smallest element in the list. It scans through all elements and returns the minimum value, which is stored in the variablesmallest
. - The
print()
function, along with an f-string, is used to display the smallest element with the message"Smallest number in the list is:"
. For the given list, the output isSmallest number in the list is: 18
.
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)
Explanation
- A list
a
is initialized with the elements[18, 52, 23, 41, 32]
. - The
sort()
method is called on the lista
, which arranges the elements in ascending order. The sorted list becomes[18, 23, 32, 41, 52]
. - The smallest number in the list is the first element, which can be accessed using the index
0
. This value is stored in the variablesmallest
. - The
print()
function is used to display the smallest element with the message"Smallest number is:"
. The output isSmallest number is: 18
.
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)
Explanation
- A list
a
is initialized with the elements[18, 52, 23, 41, 32]
. - The variable
smallest
is initialized to the first element of the list,a[0]
, if the list is not empty. If the list is empty,smallest
is set toNone
. - A
for
loop iterates through each elementi
in the lista
. - Inside the loop, an
if
statement checks if the current elementi
is smaller thansmallest
. If true,smallest
is updated toi
. - At the end of the loop,
smallest
holds the smallest number in the list. - The
print()
function is used to display the smallest element with the message"Smallest element is:"
. For the given list, the output isSmallest element is: 18
.
Output
Smallest element is: 18
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.