Python program to find smallest number in a list (original) (raw)
Last Updated : 23 Oct, 2024
In this article, we will discuss various methods to **find smallest number in a **list. The simplest way to find the smallest number in a list is by using Python’s built-in **min() function.
Using min()
The **min() function takes an iterable (like a list, typle etc.) and returns the smallest value.
Python `
a = [8, 3, 5, 1, 9, 12]
Find the smallest number
smallest = min(a) print(smallest)
`
Let us explore different methods to find smallest number in a list.
Table of Content
Using a For Loop
We can also find the smallest number in a list without using any built-in methods by using a loop (for loop). This method is useful for understanding how the comparison process works step by step.
Python `
a = [8, 3, 5, 1, 9, 12]
Initialize "smallest" value with first element of list
smallest = a[0]
Iterate through list to find smallest element
for val in a:
# If current value is smaller than current smallest value
if val < smallest:
# Update the smallest value
smallest = val
print(smallest)
`
Using Sorting
Another way to find the smallest number in a list is by sorting it. Once sorted in ascending order, the smallest number will be at the beginning of the list.
Python `
a = [8, 3, 5, 1, 9, 12] a.sort() smallest = a[0] print(smallest)
`
**Explanation:
- The sort() function sorts the list in ascending order.
- After sorting, the first element (**a[0]) will be the smallest.
**Note: This method is not recommended for finding the smallest number in a list. While it works but it is less efficient than using **min() or a **for loop. Sorting has a time complexity of **O(n log n), whereas the other methods are **O(n).
Similar Reads
- Python program to find the smallest number in a file Given a text file, write a Python program to find the smallest number in the given text file. Examples: Input: gfg.txtOutput: 9Explanation: Contents of gfg.txt: I live at 624 Hyderabad.My mobile number is 52367. My favourite number is 9.Numbers present in the text file are 9,624,52367Minimum number 3 min read
- Python3 Program to Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0Input: {0, 1, 2, 3}, n = 4, m = 5 4 min read
- Python Program to Find Largest Number in a List Finding the largest number in a list is a common task in Python. There are multiple way to do but the simplest way to find the largest in a list is by using Python's built-in max() function: Using max()Python provides a built-in max() function that returns the largest item in a list or any iterable. 3 min read
- Python Program to Find LCM of Two Numbers We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python. Example: Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelo 3 min read
- Python program to find the Decreasing point in List Given a list, get the index of element where the list shows the first negative trend, i.e first point where the next element < current element. If not found return -1. Input : test_list = [3, 6, 8, 9, 12, 5, 18, 1] Output : 4 Explanation : At 12 -> 5, first decreasing point occurs. Input : tes 4 min read
- Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4 6 min read
- Python Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9}; 4 min read
- Python program to find the smallest word in a sentence Given a string S of lowercase English alphabets, the task is to print the smallest word in the given string. Examples: Input: S = “sky is blueâ€Output: "is"Explanation: Length of “sky†is 3.Length of is “is†2.Length of “blue†is 4.Therefore, the smallest word is “isâ€. Input: S = “geeks for geeksâ€Out 5 min read
- Print all Strong Numbers in Given List - Python The task of printing all Strong numbers from a given list in Python involves iterating through the list and checking each number based on its digit factorial sum. A Strong number is a number whose sum of the factorials of its digits equals the number itself. For example, given a list a = [145, 375, 4 min read
- Sum of number digits in List in Python Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Python’s built-in functions like sum(), map(), and list comprehensions. For exa 2 min read