Finding the Smallest Element in a List
In this section, we will define a list of numbers and find the smallest element in the list.
Example
Input:
list1 = [10, 20, 4]
Output:
4
Example 1: Using Sorting
list1 = [10, 20, 4, 45, 99] list1.sort() print("The smallest element is:", *list1[:1])
Output:
The smallest element is: 4
In this example, we sort the list in ascending order and then access the first element, which will be the smallest.
Example 2: Using the min()
Method
list1 = [10, 20, 1, 45, 99] print("The smallest element is:", min(list1))
Output:
The smallest element is: 1
In this example, we use the built-in min()
function to directly find the smallest element in the list.