How to find smallest number in a Python list [9 different ways]

In this Python tutorial, I will explain how to find smallest number in a Python list step by step. Moreover, we’ll look at various examples to find the smallest value in a list.

Our objective is to create a Python program that will generate the smallest number in the given list of numbers.

We’ll go over multiple techniques that make it easy to find the smallest number in a Python list.

There are 9 different ways to find the smallest number in Python List which are shown below:

  • Using the min() function
  • Using a loop
  • Using the sort() function
  • Using sorted() function
  • Using NumPy
  • Using the reduce() function
  • Using the min() method of a set
  • Using the heapq module
  • Using lambda

How to find the smallest number in the Python list

Finding the smallest number from a given list is a simple task that can be easily accomplished using Python. we will explore multiple methods to know how to find the smallest number in Python.

Method-1: How to find the smallest number in the Python list using the min() function

Python has a built-in function min() which returns the smallest element in a list.

# Create a list of US zip codes
zip_code_USA = [36925, 85001, 96162, 19701] 

# Print the original list
print("Input list: ", zip_code_USA)

# Find the smallest number in the list using the min() function and store it in the variable new_output
new_output = min(zip_code_USA)

# Print the smallest number in the list
print("The smallest number in the input list is ", new_output)

The above code creates a list called zip_code_USA that contains several zip codes.

  • It then uses the built-in min() function to find the smallest number in the list and stores the result in a variable called new_output. Finally, the code prints the original list and the smallest number in the list using the print() function.
How to find the smallest number in a python list using min
find smallest number in a python list using min

Another example:

Let’s take an example and check how to find the smallest number in a python list using min().

Source Code:

# Creating a list
zip_code_USA = [36925,85001, 96162,19701] 
print (" Input list : ", zip_code_USA)
 
#Finding smallest number
new_output = min (zip_code_USA)
print ("The smallest number in the input list is ", new_output)

In the above code first, we created a list named “zip_code_USA” and then used the min() function and assigned the input list as an argument. After executing this code the output will display the smallest value from the list.

How to find the smallest number in a python list using min
find smallest number in a python list using min

This is how to find the smallest number in a python list using min() function.

Method-2: How to find the smallest number in a Python list using a loop

You can also use a loop to iterate through the list and keep track of the smallest number you’ve seen so far.

# Create a list of integers
my_list = [4, 6, 2, 8, 1]

# Set the variable smallest to the first element in the list
smallest = my_list[0]

# Iterate over the elements in the list using a for loop
for num in my_list:
    # Check if the current element is smaller than the current smallest element
    if num < smallest:
        # If it is, set the smallest variable to the current element
        smallest = num

# Print the smallest element in the list
print(smallest)

The code creates a list of integers called my_list and initializes a variable called smallest to the first element of the list.

  • It then iterates over each element of the list using a for loop and checks if the current element is smaller than the current value of the smallest variable.
  • If it is, the smallest variable is updated with the current element. Finally, the code prints the smallest element in the list using the print() function.
Output: 1

How to find smallest number in a Python list using a for loop

  • In this example, we will discuss how to find the smallest number in a Python list using a for loop.
  • This method compares each element individually to determine the smallest element by running a loop from the beginning to the end of the list. For this strategy, a for loop will be used.
  • Using for loop we can easily change the order of elements in the input list.

Example:

Let’s take an example and check how to find the smallest number in a Python list using a for loop.

Source Code:

def minimum(new_val):
    min= new_val[0]
    for m in new_val:
        if m<min:
            min=m
    return min

new_val=[18, 22, 64, 95, 189, 278, 123, 99, 12]
print("Input list ",new_val)
print("Smallest number :",minimum(new_val))

Create a method that will identify the lowest number. Set up a variable to hold the least value and then set its initial value to the list’s first item.

Run a loop including all list elements. Each element should be compared to the variable that contains the least value. if the element’s size is less than the variable’s value and then call the function, then print the result.

You can refer to the below Screenshot

How to find the smallest number in a Python list using a for loop
How to find smallest number in a Python list using a for loop

As you can see in the Screenshot we have discussed How to find the smallest number in a Python list using a for loop.

Method-3: How to find the smallest number in the Python list using the sort() function

We can also use the Python sort() function to sort the list in ascending order and then just take the first element as the smallest.

# Create a list of integers
new_lis = [335, 824, 912, 665, 189]

# Print the original list
print("Input list: ", new_lis)

# Sort the list in ascending order using the sort() method
new_lis.sort()

# Print the smallest number in the sorted list
print("Minimum number in the input list is ", new_lis[0])

The above code creates a list of integers called new_lis and prints the original list using the print() function.

  • It then sorts the list in ascending order using the sort() method and prints the smallest number in the list using the print() function.
Output: Input list :  [335, 824, 912, 665, 189]
Minimum number in the input list is  189

Find smallest number in a Python list using sort() – Another Example

  • Here we will discuss how to find the smallest number in a Python list using the sort() method.
  • The list elements are sorted by Python’s sort() function. The items are also arranged in ascending and descending order. Reverse, an optional argument, arranges the list in reverse, from highest to lowest. and by default list always arranges the elements in ascending order.
  • Therefore, by sorting the list, we may use indexing to access the first element in the list, which will be the list’s lowest number.

Syntax:

Here is the Syntax of the sort() method in Python

sort() 

Note: This method does not take any parameter and it will return the value.

Example:

Here we will take an example and check how to find the smallest number in a Python list using the sort().

Source Code:

#Initializing a list
new_lis = [335, 824, 912, 665, 189]
print ("Input list : ", new_lis)
 
#sorting the list elements
new_lis.sort ()
 
#Display smallest number
print ("Minimum number in the input list is ", new_lis[0])

In the above code first we initialized the list and then used the list.sort() function use indexing to access the first element in the list, which will be the list’s lowest number.

Here is the Screenshot of the following given code

Find the smallest number in a Python list using sort
Find smallest number in a Python list using a sort()

Read: Min() Function in Python

In this example, we have understood how to find the smallest number in a Python list using a sort().

Method-4: How to find the smallest number in the Python list using sorted() function

You can use the sorted() function to sort the list in ascending order, and then just take the first element, which will be the smallest.

# Create a list of integers
my_list = [4, 6, 2, 8, 1]

# Sort the list in ascending order using the built-in sorted() function and store the result in a new variable called sorted_list
sorted_list = sorted(my_list)

# Set the variable smallest to the first element in the sorted list
smallest = sorted_list[0]

# Print the smallest element in the list
print(smallest)

The above code creates a list of integers called my_list and uses the built-in sorted() function to sort the list in ascending order, storing the result in a new variable called sorted_list.

  • It then sets the variable smallest to the first element of the sorted list and prints it using the print() function.
Output: 1

Method-5: How to find the smallest number in the Python list using NumPy

If you have the NumPy library installed, you can use its amin() function to find the minimum element in a list.

# Import the numpy library and give it the alias np
import numpy as np

# Create a list of integers
my_list = [14, 6, 2, 8, 11]

# Use the numpy amin() function to find the smallest element in the list and store the result in a variable called smallest
smallest = np.amin(my_list)

# Print the smallest element in the list
print(smallest)

The above code imports the numpy library and gives it the alias np.

  • It then creates a list of integers called my_list. The code uses the np.amin() function from the numpy library to find the smallest element in the list and stores the result in a variable called smallest.
  • Finally, the code prints the smallest element in the list using the print() function.
Output: 2

Method-6: How to find the smallest number in the Python list using the reduce() function

You can use the reduce() function from the functools module to iteratively apply a function to pairs of elements in the list until you’re left with a single value.

# Import the reduce() function from the functools library
from functools import reduce

# Create a list of integers
my_list = [5, 16, 21, 18, 12]

# Use the reduce() function to apply a lambda function that returns the smallest element in the list to the list and store the result in a variable called smallest
smallest = reduce(lambda x, y: x if x < y else y, my_list)

# Print the smallest element in the list
print(smallest)

The above code imports the reduce() function from the functools library, which is a higher-order function that applies a binary function to the elements of a list in a cumulative way. It then creates a list of integers called my_list.

  • The code uses the reduce() function and a lambda function to find the smallest element in the list and stores the result in a variable called smallest. Finally, the code prints the smallest element in the list using the print() function.
Output: 5

Method-7: How to find the smallest number in the Python list using the min() method of a set

If you convert the list to a set, you can use the min() method of the set to find the smallest element.

# Create a list of integers
my_list = [5, 16, 21, 18, 12]

# Use the built-in set() function to remove duplicates from the list, then use the built-in min() function to find the smallest element in the remaining set, and store the result in a variable called smallest
smallest = min(set(my_list))

# Print the smallest element in the list
print(smallest)

The code creates a list of integers called my_list. It uses the built-in set() function to remove duplicates from the list.

  • Then uses the built-in min() function to find the smallest element in the remaining set, and stores the result in a variable called smallest. Finally, the code prints the smallest element in the list using the print() function.
Output: 5

Method-8: How to find the smallest number in the Python list using the heapq module

If you have a very large list and want to find the smallest element without sorting the entire list, you can use the heapq module to create a heap and find the smallest element in constant time.

# Import the heapq library, which provides an efficient way to find the smallest elements in a list
import heapq

# Create a list of integers
my_list = [5, 16, 21, 18, 12]

# Use the nsmallest() function from the heapq library to find the smallest element in the list and store the result in a variable called smallest
smallest = heapq.nsmallest(1, my_list)[0]

# Print the smallest element in the list
print(smallest)

The above code imports the heapq library, which provides an efficient way to find the smallest elements in a list. It then creates a list of integers called my_list.

  • The code uses the nsmallest() function from the heapq library to find the smallest element in the list and stores the result in a variable called smallest. Finally, the code prints the smallest element in the list using the print() function.
Output: 5

Method-9: Find smallest number in a Python list using lambda

  • In this section, we will discuss how to Find the smallest number in a Python list using lambda.
  • A lambda function only has one expression but can accept any number of arguments. An expression is a line of code that the lambda function executes and which might or might not yield any results.

Example:

Let’s take an example and check how to find the smallest number in a Python list using lambda

Source Code:

United_states_of_America = [345, 192, 145, 298, 82]
print(min(United_states_of_America , key=lambda value: int(value)) )

In the above code first we created the list and assign the integer numbers and then used the lambda function to get the minimum number from the list.

Here is the execution of the following given code

Find the smallest number in a Python list using lambda
Find smallest number in a Python list using lambda

You may also like the following Python list tutorials:

Conclusion

In this Python tutorial, we have covered 9 different methods to find the smallest number in a Python list by following the below methods:

  • Using the min() function
  • Using a loop
  • Using the sort() function
  • Using sorted() function
  • Using NumPy
  • Using the reduce() function
  • Using the min() method of a set
  • Using the heapq module