Python how to find the smallest number in a list


Python list method min() returns the elements from the list with minimum value.

Syntax

Following is the syntax for min() method −

min(list)

Parameters

  • list − This is a list from which min valued element to be returned.

Return Value

This method returns the elements from the list with minimum value.

Example

The following example shows the usage of min() method.

#!/usr/bin/python list1, list2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200] print "min value element : ", min(list1) print "min value element : ", min(list2)

When we run above program, it produces following result −

min value element : 123 min value element : 200

python_lists.htm

In this tutorial, we will see various Python programs to find the smallest number in a List. For example, if the list is [15, 20, 10, 16] then the program should display number 10 as the output (the smallest number in the given list).

Example 1: Finding smallest number in a list using sort() method

In the following program, we are finding the smallest number in the given list using sort() function. The sort() function sorts the given list in ascending order. We are then displaying the first element of the list, which is the smallest number of the sorted list.

# Python program to find smallest number in a list # Given list of numbers lis = [89, 100, 35, 16, 99] # sorting the given list "lis" # sort() function sorts the list in ascending order lis.sort() # Displaying the first element of the list # which is the smallest number in the sorted list print("Smallest number in the list is:", lis[0])

Output:

Python how to find the smallest number in a list

Example 2: Finding smallest number in a list using min() method

In the following program we are finding the smallest number of the list using min() method. The min() method returns the smallest element of the list.

# Python program to find smallest number in the # given list using min() method # Given list of numbers lis = [9, 100, 3, 16, 60] # min() method returns the smallest element of the list print("Smallest number of the list is:", min(lis))

Output:

Python how to find the smallest number in a list

Example 3: Finding smallest number in a list, where list is provided by user

In the following program we are finding the smallest number in the user provided list, user is asked to enter the number of elements to put in a list. User then enters the elements of the list one by one, each input element is appended to the list using append() method. At the end of the program, the smallest number of the list is displayed using min() method.

# creating empty list lis = [] # user enters the number of elements to put in list count = int(input('How many numbers? ')) # iterating till count to append all input elements in list for n in range(count): number = int(input('Enter number: ')) lis.append(number) # displaying smallest element print("Smallest element of the list is :", min(lis))

Output:

Python how to find the smallest number in a list

Related Python Examples

Many careers in tech pay over $100,000 per year. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech.

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Last update on May 28 2022 13:15:45 (UTC/GMT +8 hours)

Write a Python program to get the smallest number from a list.

Example - 1 :

Python how to find the smallest number in a list

Example - 2 :

Python how to find the smallest number in a list

Example - 3 :

Python how to find the smallest number in a list

Example - 4 :

Python how to find the smallest number in a list

Sample Solution:

Python Code:

def smallest_num_in_list( list ): min = list[ 0 ] for a in list: if a < min: min = a return min print(smallest_num_in_list([1, 2, -8, 0]))

Sample Output:

-8

Flowchart:

Python how to find the smallest number in a list

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get the largest number from a list.
Next: Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.

What is the difficulty level of this exercise?

How do I sort a dictionary by value?

Python 3.6+

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} {k: v for k, v in sorted(x.items(), key=lambda item: item[1])} {0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

Older Python

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list-probably a list of tuples.

import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(1))

sorted_x will be a list of tuples sorted by the second element in each tuple. dict(sorted_x) == x.

And for those wishing to sort on keys instead of values:

import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(0))

In Python3 since unpacking is not allowed [1] we can use

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=lambda kv: kv[1])

If you want the output as a dict, you can use collections.OrderedDict:

import collections sorted_dict = collections.OrderedDict(sorted_x)

Ref: https://bit.ly/3dJyjpV

You can find the smallest number of a list in Python using min() function, sort() function or for loop.

Python how to find the smallest number in a list

We shall look into the following processes to find the smallest number in a list, with examples.

  • Use min() builtin function
  • Use List sort() function
  • Use Python For Loop

Choose one, based on your program requirements or your own personal performance recommendations.

Example 1: Find the smallest number using min()

min() function can take a list as argument and return the minimum of the elements in the list.

In this example, we will take a list of numbers and find the smallest of the list using min() function.

Python Program

# python program to find the smallest number # list of numbers a = [18, 52, 23, 41, 32] # find smallest number using min() function smallest = min(a) # print the smallest number print(f'Smallest number in the list is : {smallest}.') Run

Output

Smallest number in the list is : 18.

Example 2: Find the smallest number using sort() function

We know that sort() function sorts a list in ascending or descending order. After you sort a list, you have your smallest number at the start of the list if you have sorted in ascending order or at the end of the list if you have sorted in descending order.

Python Program

# python program to find the smallest number # list a = [18, 52, 23, 41, 32] # sort the list, default is in ascending order a.sort() # smallest number smallest = a[0] # print the smallest number print("Smallest number is: ",smallest) Run

Output

Smallest number is: 18

Example 3: Find the smallest number using for loop

Though finding the smallest number using sort() function is easy, using Python For Loop does it relatively faster with less number of operations. And also, we do not change the order of elements in the given list.

Python Program

a=[18, 52, 23, 41, 32] # smallest number smallest = a[0] if a else None # find smallest for i in a: if i<smallest: smallest=i print("Smallest element is: ", smallest) Run

Output

Smallest element is: 18

In this example,

  1. We have assumed a list and initialized largest number variable to first element of the list. If the list is empty, smallest number will be None.
  2. Then we iterated over the list using for loop.
    • During each iteration, we checked if the smallest number is greater than the element. If that is the case, we assigned our smallest number with the element.
  3. When you complete traversing the list, you will end up with the smallest number of the list in your variable.

Summary

In this tutorial of Python Examples, we learned some of the ways to find the smallest number in a Python list with the help of well detailed Python example programs.