Python program to find smallest number in a list
In this tutorial, you will learn how to find the smallest number in a list. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. In this tutorial, we will be discussing the various approaches by which we can find the smallest element in the given list.
For a given list of numbers, the task is to find the smallest number in the list.
Input: [10, 3, 20, 9, 11, 15, 23, 6]
Output: 3
Approach to find smallest number in a list
For executing this program in Python, there are multiple approaches we can follow:
- By comparing each element for finding the smallest number
- By using min() function
- By sorting the list using sort() function and printing the smallest element
Let us look at each approach in detail.
Approach 1: Comparing elements
In this approach, we will run a loop from the starting to the ending of the list and compare each element individually to find the minimum element. We will use for loop in this approach.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Define a function that will find the smallest number
Step 2- Declare a variable that will store the smallest value
Step 3- Initialise it to the first value in list
Step 4- Run a loop for all elements in list
Step 5- Compare each element with the variable which stores the smallest value
Step 6- If the element is smaller than the value in the variable
Step 7- Copy the element in the variable
Step 8- Return the variable
Step 9- Declare and initialize list or take input
Step 10- Call function and print output
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
#smallest element in list
#function
def smallest(list):
small= list[0]
for i in list:
if i<small:
small=i
return small
#list
list=[3, 9, 7, 3, 6, 5, 7, 24, 6]
print("smallest in ",list,"is")
print(smallest(list))
smallest in [3, 9, 7, 3, 6, 5, 7, 24, 6] is
3
We have used '\n' for printing the statement in the next line. It is an escape sequence new line character.
Approach 2: min() function
In this approach, we will use min() method which is a built-in function in the Python library. It will return the smallest value in the list.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Declare a function that will find the smallest number
Step 2- Use min() method and store the value returned by it in a variable
Step 3- Return the variable
Step 4- Declare and initialize a list or take input
Step 5- Call the function and print the value returned by it
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
#smallest element in list
#function
def smallest(list):
small= min(list)
return small
#input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
#smallest
print("smallest in ",li,"is")
print(smallest(li))
Enter size of list4
Enter element of list 9
Enter element of list 4
Enter element of list 29
Enter element of list 4
smallest in [9, 4, 29, 4] is
4
Approach 3: by sorting and printing the smallest number
In this approach, we will first sort the elements of the list. For sorting the list we will use the predefined sort() function in Python. It will sort the elements in the list in ascending order. We will print the element present at the first position after sorting. This will be the smallest element in the list.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Declare a function for finding the smallest number
Step 2- Use sort() method to sort the list
Step 3- Return the first element in the list after sorting
Step 4- Declare a list and take input or initialize values
Step 5- Call the function
Step 6- Print the value returned by the function
Python Program3
Look at the program to understand the implementation of the above-mentioned approach.
#smallest element in list
#function
def smallest(list):
list.sort()
return list[0]
#input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
#smallest
print("smallest in ",li,"is")
print(smallest(li))
Enter size of list 5
Enter element of list 3
Enter element of list 9
Enter element of list 6
Enter element of list 0
Enter element of list 6
smallest in [3, 9, 6, 0, 6] is
0
Conclusion
So far, we have discussed three ways by which we can find the smallest element in the list. We have discussed how to use a for loop for comparing each element in the list and finding the minimum element. We have also used built-in functions like min() and sort() to find the smallest element in the list.