How to find the largest number in a list Python using for loop exampleSimple example code Show
Output: Or use this code (a bit complicated) a = [18, 52, 23, 41, 32] # variable to store largest number ln = a[0] if a else None # find largest number for i in a: if i > ln: ln = i print("Largest element is: ", ln)Output: Largest element is: 52 Do comment if you have any doubts or suggestions on this Python list topic.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical. Share this:
RelatedFind Largest Number in Python ListYou can find largest number of a list in Python using sort() function or more basic for loop. Using sort() function is kind of concise, but using For Loop is efficient. We will go through these two approaches with examples. Example 1: Find the largest number using sort() functionWe know that sort() builtin function sorts a list in ascending or descending order. After you sort a list, you have your largest number at the end of the list if you have sorted in ascending order or at the start of the list if you have sorted in descending order. In the following example, we will sort the given list in ascending order. Of course, last number of the sorted list is our required largest number. Python Program # list a=[18, 52, 23, 41, 32] # sort the list, default is in ascending order a.sort() # largest number is the last item in the sorted list ln = a[-1] # print the largest number print("Largest element is: ",ln)RunOutput Largest element is: 52a[-1] fetches the last element in the list. Example 2: Find Largest Number in List using For LoopThough finding the largest number using sort() function is easy, using Python For Loop does it relatively faster with less number of operations. Also, the contents of the list are unchanged. Python Program a = [18, 52, 23, 41, 32] #variable to store largest number ln = a[0] if a else None #find largest number for i in a: if i>ln: ln=i print("Largest element is: ",ln)RunOutput Largest element is: 52In this example, we have assumed a list and initialized largest number ln variable to the first element of the list. If there are no elements in the list, ln is initialized with None. Iterate the loop for each element in the list. During each iteration, we check if the largest number is less than this element. If that is the case, we update our largest number with the element. When you complete traversing the list, you will end up with the largest number of the list in your variable. SummaryIn this tutorial of Python Examples, we learned some of the ways to find largest number in Python list with the help of well detailed Python example programs. Related Tutorials
Python Program to find the Largest Number in a List Example 1The Python max function returns the maximum value in a List. # Python Program to find Largest Number in a List a = [10, 50, 60, 120, 20, 15] print("The Largest Element in this List is : ", max(a))Python largest list number output The Largest Element in this List is : 120IntroductionIn this section of python programming, we will understand the different methods to find the largest number in the list. Smallest and Largest number in list — programminginpython.comHere we use 2 predefined functions min() and max() which check for the smallest and largest number in a list respectively. Python program to find largest number in a listGiven a list of numbers, the task is to write a Python program to find the largest number in given list. Examples: Input : list1 = [10, 20, 4] Output : 20 Input : list2 = [20, 10, 20, 4, 100] Output : 100Method 1 : Sort the list in ascending order and print the last element in the list.
Output: Largest element is: 99Method 2 : Using max() method
Output: Largest element is: 99Method 3 : Find max list element on inputs provided by user
Output: Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Largest element is: 99Method 4 : Without using built in functions in python:
Output: Largest element is: 99Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Article Tags :
Python Python Programs
Python list-programs python-list Practice Tags :
python-list The objective of this Python post, you will see various Python programs that cover the following:
Python program to find largest of n numbers using max
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst))
Output How many numbers: 5
Enter number 4
Enter number 6
Enter number 8
Enter number 9
Enter number 10
Maximum element in the list is : 10
Recommended:-Python Program to Calculate n-th term of a Fibonacci Series Python program to find largest of n numbers without using built-in function
def find_max( list ):
max = list[ 0 ]
for a in list:
if a > max:
max = a
return max
num = int(input('How many numbers: '))
lst = []
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", find_max(lst))
Output How many numbers: 3
Enter number 1
Enter number 5
Enter number 7
Maximum element in the list is : 7
|