In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a list, we need to calculate the largest element of the list.
Here we will take the help of built-in functions to reach the solution of the problem statement
Using sort() function
Example
# list list1 = [23,1,32,67,2,34,12] # sorting list1.sort() # printing the last element print("Largest element is:", list1[-1])
Output
Largest in given array is 67
Using max() function
Example
# list list1 = [23,1,32,67,2,34,12] # printing the maximum element print("Largest element is:", max(list1))
Output
Largest in given array is 67
We can also take input from the user by the code given below
Example
# empty list list1 = [] # asking number of elements to put in list num = int(input("Enter number of elements in list: ")) # appending elements in the list for i in range(1, num + 1): ele = int(input("Enter elements: ")) list1.append(ele) # print maximum element print("Largest element is:", max(list1))
Conclusion
In this article, we have learned how to get the largest element from the list.