How to Find the Smallest Number in a Python List [8 Ways]

In this Python tutorial, I will explain how to find the smallest number in a Python list. We will use different methods to write a Python program to find smallest number in a list.

We aim to create a Python program to generate the smallest number in the given list of numbers.

We’ll review multiple techniques that make finding the smallest number in a Python list easy.

During my research, I found eight methods to find the smallest number in a list in Python. Here is the list:

  • Using the min() function
  • Using a loop
  • Using the sort() function
  • Using sorted() function
  • Using lambda function
  • Using reduce() function
  • Using recursion
  • Using the heapq module

Find the smallest number in a Python list

A list is a collection of items in Python. To find the smallest number in a list, we can use the min() function, which gives the smallest number from the passed list.

We can also sort the list and then extract the first or last element according to the order using different methods in Python.

Let’s see them all in detail with examples:

1. Python program to find smallest number in a list using the min() function

Python has a built-in min() function, which returns the smallest item in an iterable or the smallest of two or more arguments. We can use the min() function in the Python list to find the smallest number.

Here is an instance:

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

For the above code, we have created a Python 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.
Input list:  [36925, 85001, 96162, 19701]
The smallest number in the input list is  19701

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to find smallest number in a Python list

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

2. Find smallest number in Python using for loop

We can also use a for loop in Python to iterate through each element of the Python list and compare it with the smallest number found.

We will use the if conditional statements in Python to check for the smallest number while comparing.

Here is an example to find smallest number in Python using for loop:

populations = [39512223, 57875913, 21477737, 10617423, 19453561]
smallest_population = populations[0]
for num in populations:
    if num < smallest_population:
        smallest_population = num
print("State with the smallest population:", smallest_population, "people")

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

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

Output:

State with the smallest population: 10617423 people

Below is a screenshot displaying the output after implementing the Pycharm editor’s code.

how to find the smallest number in a list python using for loop

3. Python program to find smallest number in a list using the sort() function

We can also use the Python sort() function to sort the elements of a list in ascending order. We can find the smallest number by sorting the list and selecting the first element by indexing in Python.

Let’s see an example of using the sort() function to find the smallest number in a Python list.

temperatures_nyc = [32, 28, 30, 29, 31]
temperatures_nyc.sort()
lowest_temp = temperatures_nyc[0]
print("Lowest temperature in NYC:", lowest_temp, "°F")

The above code creates a list of integers called temperatures_nyc.

  • Then, sort the list in ascending order using the sort() method in Python and extract the first element from the sorted list.

Output:

Lowest temperature in NYC: 28 °F

Upon running the code in Pycharm, the resulting output is displayed in the screenshot below.

how to get smallest number in list python

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

4. How to find smallest number in list Python using the sorted() function

We can use the sorted() function in Python to sort the list in ascending order and then take the first element, which will be the smallest, similar to the last method.

The sorted() function returns a sorted list of the specified iterable object. We can also sort the list in descending order and extract the last element, as it will be the smallest element

Here is an instance to get the smallest item in the Python list:

employee_ages = [28, 29, 20, 37, 41]
sorted_employee_age = sorted(employee_ages, reverse=True)
youngest_employee = sorted_employee_age[len(sorted_employee_age)-1]
print("Youngest employee in the organization:", youngest_employee, "years old")

The above code creates a list of ages in numbers called employee_ages.

We use the built-in sorted() function with a reverse parameter to sort the list in descending order, storing the result in a new variable called sorted_employee_age.

sorted(employee_ages, reverse=True)

Then, we access the last element of the sorted list by indexing in Python.

sorted_employee_age[len(sorted_employee_age)-1]
  • The len() function in Python returns the length of the list, and as the indexing starts from 0, we subtract one (1) from the length to get the last element.
  • Note: We can also use the pop() method to get the last element from the sorted list in Python.

Output:

Youngest employee in the organization: 20 years old

The screenshot below presents the output after the code was successfully implements the Pycharm editor.

python smallest number in list

5. How to find smallest element in list Python using lambda function

  • This section will discuss how to find the smallest number in a Python list using the lambda function.
  • A lambda function only has one expression. They can be used to create a function object, which might or might not yield results.
  • We can combine it with the min() function to find the lowest number in the Python list.

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

Source Code:

altitudes = [14440, 14420, 'Null', 14050, 14265, 14300, 'Null']
lowest_altitude = min(altitudes, key=lambda x: float(x) if str(x).isdigit() else float('inf'))
print("Mountain with the lowest altitude in Colorado:", lowest_altitude, "feet")

Here, we have used the lambda function to filter the list of integers and then find the minimum value among the filtered list elements in Python using the min() function.

min(altitudes, key=lambda x: float(x) if str(x).isdigit() else float('inf'))

Output:

Mountain with the lowest altitude in Colorado: 14050 feet

Here is the execution of the following given code:

how to find smallest number in python list

6. How to find the smallest number in a list Python using the reduce() function

You can use the reduce() function from the functools module of Python to iteratively apply a function to pairs of elements in the list until we are left with a single value, i.e., the smallest number from a list in Python.

Below is an example that explains how to use the reduce() function in Python to get the smallest number in a list.

from functools import reduce

production = [12, 89, 64, 17, 78]
lowest_production = reduce(lambda x, y: x if x < y else y, production)
print("Day with the least production in Texas had done only", lowest_production, "percent.")

The above code imports the reduce() function from the functools library in Python.

  • The reduce() function applies a lambda function to find the smallest element in the list using comparison operators and stores the result in a variable called lowest_production.
  • Finally, the code prints the smallest element in the list using the print() function.

Output:

Day with the least production in Texas had done only 12 percent.

The screenshot below features the output after executing the Pycharm editor’s code.

write a python program to get the smallest number from a list

7. Find smallest number in the list Python using recursion

We can also use the recursion technique to find the smallest number in a list in Python.

Recursion is a technique where a function calls itself in its definition.

Here is an example:

def find_cheapest_car(prices):
    if len(prices) == 1:
        return prices[0]
    else:
        return min(prices[0], find_cheapest_car(prices[1:]))

car_prices = [25000, 28000, 22000, 26000, 20000]
cheapest_car = find_cheapest_car(car_prices)
print("Cheapest car price in New York City:", cheapest_car, "dollars")

Here, we recursively compare the first element of the list to the rest of the elements using the min() function in Python until we are left with a single element in the list, which is the smallest number in the Python list.

min(prices[0], find_cheapest_car(prices[1:]))

Output:

Cheapest car price in New York City: 20000 dollars

After executing the code in Pycharm, one can see the output in the below screenshot.

write a python program to get the smallest number from a list.

8. How to find the smallest number in a list Python using the heapq module

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

Here is an instance to find the smallest number in the Python list:

import heapq

rates = [3.2, 4.1, 2.9, 3.5, 3.8]
heapq.heapify(rates)
lowest_rate = heapq.heappop(rates)
print("The lowest rate is", lowest_rate, "%")

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 heapify() function from the heapq library in Python to transform a regular list into a heap. In other words, it rearranges the list in place into a Min heap.
  • Then, the heappop() function pops the smallest element while preserving the heap property.
heapq.heapify(rates)
lowest_rate = heapq.heappop(rates)
  • Finally, the code prints the smallest element in the list using the print() function.

Output:

The lowest rate is 2.9 %

Below is a screenshot displaying the output after implementing the Pycharm editor’s code.

python print smallest number in list

Conclusion

This tutorial contains eight methods to find the smallest number in a Python list, like the min() function, a for loop, sort() function, sorted() function, lambda function, reduce() function, recursion, and different functions of the heapq module.

Depending on the size of the Python list and specific requirements, one can choose the most suitable method for their task.

You may also like the following Python list tutorials: