Write a python program to get the largest number from a list
In this post, you will learn different ways to get the largest number from a list using the Python programming language. Such a type of question is generally asked in programming interviews or competitive exams. Suppose the list is [32, 34, 23, 10], then the output of the program or the largest number in the given list is 34. In Python, there are different ways to find the largest number from a list. Here, we have mentioned most of them.
Python find largest number in list using sort() method
In the given code, a list of numbers is given and we are using the sort() function to get the largest number from a list. We sort the list in ascending order. So that we can get the last element of the list, which is the largest number in the list.
# Python program to find largest number in a list
# A list of numbers is given
list_x = [41, 4, 20, 21, 14, 72]
# sorting the given list in
# ascending order
list_x.sort()
# displaying the last element of the list
print("Largest number in the list is:", list_x[-1])
Output of the above code-
Largest number in the list is: 72
Python find largest number in list using max() function
The max() function returns the largest number in the list. In the given program, a list of numbers is given and we are getting the largest number with the help of the max() function.
# Python program to find largest number in a list
# A list of numbers is given
lst = [1, 42, 52, 13, 53, 92, 4]
# max() method returns the largest element of the list
# displaying the last element of the list
print("Largest number in the list is:", max(lst))
Output of the above code-
Largest number in the list : 92
Python find largest number in list using max() with user inputs
This python program is the same as the above, but here we allow the user to provide a total number of elements to put in the list and, then enter the elements of the list which are appended to the list using the append() method. At the end, we are getting the largest number with the help of the max() function.
# Python Program to find Largest Number in a List
# creating empty list
num_list = []
# user enters the number of elements to put in list
count = int(input('How many numbers? '))
# iterating till num to append all input elements in list
for n in range(count):
num = int(input('Enter number: '))
num_list.append(num)
# printing largest element
print("Largest element of the list is :", max(num_list))
Output of the above code-
How many numbers? 5
Enter number: 23
Enter number: 12
Enter number: 442
Enter number: 12
Enter number: 421
Largest number in the list is: 442
Python find largest number in list using the lambda function
A lambda function is a small anonymous function that takes any number of arguments, but can only have one expression. Here, we have used the lambda function to find largest number in list.
lst = [89, 30, 130, 20, 100]
print("Largest element of the list:",max(lst, key=lambda value: int(value)) )
Output of the above code:
Largest element of the list: 130
Related Articles
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword