How to find smallest number in a Python list

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 smallest number in a Python list.

Here we will discuss

  • How to find smallest number in a python list using min()
  • Find the smallest number in a Python list using sort()
  • How to find the smallest number in a python list using a for loop
  • Find the smallest number in a Python list using lambda

Find smallest number in a Python list

In Python, there are numerous methods for finding the smallest number in a list. We’ll go through how to find the smallest number in a Python list specifically utilizing the min() function, sort() function, and for loop.

How to find smallest number in a python list using min()

  • In this section, we will discuss how to find the smallest number in a python list using a min() method in Python.
  • Python has a built-in function called min() that finds the smallest number in a list as an argument.
  • It takes a list as a parameter and returns the smallest of the element in the given list.

Syntax:

Let’s have a look at the syntax and understand the working of the min() function in Python

min(list)

Note: This is a list from which to return elements with minimum values.

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 smallest number in a python list using min()

Read: Python list methods

Find smallest number in a Python list using sort()

  • 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()

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

Read: Python concatenate list

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.

Read: How to find a string from a list in Python

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

Also, take a look at some more Python tutorials.

In this article, we have discussed several different ways to find the smallest number in a Python list. The following topic we have covered.

  • How to find smallest number in a python list using min()
  • Find smallest number in a Python list using sort()
  • How to find smallest number in a python list using a for loop
  • Find smallest number in a Python list using lambda