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