Python Program to find Largest Number in a List

Write a Python Program to find the Largest Number in a List with a practical example. The built-in max() function returns the highest list item, the sort() function sorts the items ascending, and the last item will be the largest. Apart from these two, you can use the for loop or while loop to iterate the list items and if statement to find the largest.

Python Program to find the Largest Number in a List using max()

The max function returns the maximum value in a List.

a = [10, 50, 60, 120, 20, 15]

print(max(a))

Output

120

max() function Example 2

This program for the maximum list number is the same as above. But this time, we are allowing the user to enter the length of a List. Next, we used For Loop to add numbers to the Python list.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("The Largest Element in this List is : ", max(NumList))
Python Program to find Largest Number in a List 2

Python Program to find the Largest Number in a List Using the sort function

The sort function sorts the List elements in ascending order. Next, we use the Index position to print the Last element in a List.

a = [10, 50, 60, 80, 20, 15]

a.sort()
print(a[5])

Output

80

Using sort() function example 2

This largest list number program is the same as above. But this time, we allow the user to enter their own list of items.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()

print("The Largest Element in this List is : ", NumList[Number - 1])
Python Program to find the Largest Number in a List using sort

Python Program to find the Largest Number in a List using sort and reverse

This program sorts the list items in Ascending order. Next, we used the reverse function to reverse the list of items. Lastly, we used index position 0 to print the first element in a list.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()
NumList.reverse()

print("The Largest Element in this List is : ", NumList[0])
Python Program to find the Largest Number in a List using sort and reverse functions

Python Program to find the Largest Number in a List using for loop

In this program, we are not using any built-in function such as sort, reverse, or max function

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

largest = NumList[0]    
for j in range(1, Number):
    if(largest < NumList[j]):
        largest = NumList[j]
        position = j

print("The Largest Element in this List is : ", largest)
print("The Index position of the Largest Element is : ", position)
Python Program to find the Largest Number in a List using for loop

The User inserted values of the Python Program to find the Largest Number in a List are
NumList[5] = {70, 80, 120, 87, 46}
largest = NumList[0] = 70

First Iteration – for 1 in range(1, 5) – Condition is true
So, it executes the If statement inside the loop until the condition fails.

if (largest < NumList[j]) inside the for loop is True because (70 < 80)
largest = NumList[1]
largest = 80
position = 1

Second Iteration: for 2 in range(1, 5) – Condition is true
If (largest < NumList[2])  =  (80 < 120) – Condition True
largest = NumList[2] = 120
Position = 2

Third Iteration: for 3 in range(1, 5) – Condition is true
If (largest < NumList[3])  =  (120 < 87) – Condition False
largest = 120
Position = 2

Fourth Iteration: for 4 in range(1, 5) – Condition is true
If (largest < NumList[4])  =  (120 < 46) – Condition False
largest = 120
Position = 2

Fifth Iteration: for 5 in range(1, 5) – Condition is False. So, it exits from the loop.