Python Program to Find smallest number in List

Python

In this post, we are going to explore Python Program to Find smallest number in List , program to find smallest number using recursion, find smallest number using for loop entered by the user, find smallest program using min() and sort(), find smallest number without built-in function.

1.Python Program to Find smallest number in List using for loop


In this Python program, we are asking users to input the elements of the list. Then for loop to iterate over elements entered by user and append to the list. We are using the built-in min() method to get the smallest number.

# create an empty list
resList = []



# enter  number of elements in input 
num = int(input("Enter number of elements for list : "))

   
# running loop till the number of element
for i in range(0, num):
    element = input('enter list element:')  
    
     # appending  element one by one
    resList.append(element)

print("The Smallest number in list is :", min(resList))

Output

Enter number of elements for list : 5
enter list element:12
enter list element:14
enter list element:700
enter list element:60
enter list element:50
The Smallest number in list is : 12

2. Python Program to Find smallest item in List recursively


In this example, we have defined the function minVal_recursive() to find the smallest number calling it recursively.

reslist = [12,14,700,60,50]

def minVal_recursive(resList):

 if len(resList)==2: 

    if resList[0]<resList[1]:
        return resList[0]
    else:
        return resList[1]

 else:
    min_val= minVal_recursive(resList[1:])
    if resList[0]<min_val:
        return resList[0]
    else:
        return min_val

print("The Smallest number in list is :", minVal_recursive(resList))

Output

The Smallest number in list is : 12

3.Python Program to find smallest item in List using min()


In this program, we are using the built-in min() function and passing list as an input.

reslist = [12,14,700,60,50]


print("The Smallest number in list is :", min(reslist))

Output

The Smallest number in list is : 12

4.Python Program to Find smallest number in List


In this program, we have called the list sort() method and accessed the first element to get the smallest element.

reslist = [12,14,700,60,50]

reslist.sort()
print("The Smallest number in list is :", reslist[0])


Output

The Smallest number in list is : 12

5. Python program to Find smallest item in List


In this program, we have defined custom function lst_minVal() inside the function using for loop to iterate over all elements and comparing elements to get a minimum number in the list.

reslist = [12,14,700,60,50]




def lst_minVal(list):
  min_val = list[0]
 
 for item in range(len(list)):
 
   
    if list[item] < min_val:
        min_val = list[item] 
 
 print("The smallest element in the list is ",min_val)

Output

The Smallest element in list is : 12

Summary

In this post we have explored Python Program to Find smallest number in List , program to find smallest number using recursion, find smallest number using for loop entered by the user, find smallest program using min() and sort(), find smallest number without built-in function.