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.

Python Program to find the Largest Number in a List Example 1

The max function returns the maximum value in a List.

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

print(max(a))

Output

120

Largest Number in a List 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

Using sort function Example 3

The Python 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

Example 4

This largest list number program is the same as above. But this time, we allow the user to enter their own list 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])

Output

Please enter the Total Number of List Elements: 3
Please enter the Value of 1 Element : 90
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 70
The Largest Element in this List is :  90

Example 5

This program sorts the list items in Ascending order. Next, we used the reverse function to reverse the list 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])

output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 60
Please enter the Value of 2 Element : 30
Please enter the Value of 3 Element : 90
Please enter the Value of 4 Element : 89
Please enter the Value of 5 Element : 45
The Largest Element in this List is :  90

Python Program to find 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)

Output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 70
Please enter the Value of 2 Element : 80
Please enter the Value of 3 Element : 120
Please enter the Value of 4 Element : 87
Please enter the Value of 5 Element : 46
The Largest Element in this List is :  120
The Index position of the Largest Element is :  2

The User inserted values 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 starts executing 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]
largest = 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.