Python Program to find the largest number in the list

In this article, we are going to perform a simple task, in which, we have a list, and in that list, we need to find the largest number. We will have a look at some different ways, through which, we can find the largest number in the list.

Have a look at the below input-output examples, which would give us a clear idea about our goal.

Python Program to find the largest number in the list

Example 1 –

Input – [11, 32, 55, 34, 12, 4, 22]
Output – Largest element in the list is: 55

Example 2 –

Input – [34, 23, 65, 6, 76,45, 34]
Output – Largest element in the list is: 76

As you can see in the above input outputs, we get a clear idea about what we are required to do.

The thing is that there can be multiple ways to find the largest number in the list, and here, we are going to have a look at some simple approaches to finding the largest number in the list.

Way 1 – Using the max function

Using the max function, we get the largest number in the given iterable(list in our case). So, using the max function, we are going to get the largest element in the given list. Let’s have a look at the below program, which tries to demonstrate the same thing –

numbers = [11, 32, 55, 34, 12, 4, 22]
largest = max(numbers)
print(f”The largest number in the list is:{largest}”)

As you can see in the above program, we have a list, in which, we are having some numbers. After that, we are using the max function, to get the max value from the given list. After that, we are just printing the largest number.
Now, let’s have a look at the output of the above program.

Output

The largest number on the list is:55

As you can see, we got the largest number on the list. You can also execute the program for some different values in the list, and observe the output.

So, we can simply use the max function, to get the largest number in the list in Python.

Way 2 – Sort the list in the ascending order

Here, we can get the largest number in the list, by sorting the list in ascending order. If we sort the list in ascending order, the largest number in the list is going to be the last element in the sorted list. To sort the list, we are going to make use of the sort method. Let’s have a look at a simple program, which tries to demonstrate the same thing.

numbers = [34, 23, 65, 6, 76,45, 34]
# sorting the list
numbers.sort()
# The largest number is the last element in the sorted list.
print(f”The largest number in the list is:{numbers[-1]}”)

As you can see in the above program, we have a list, in which, we have numbers, and then we are just sorting the list. The largest number in the list is the last element in the sorted list. We are printing that element as our output.

Let’s have a look at the output of the above program –

Output – 

The largest number on the list is:76

As you can see, we have the largest number from our list as our output. You can also try changing the list elements, and executing the program yourself.

So, by sorting the list in ascending order, we can get the largest number in the list, as the last element in the sorted list.

Way 3 – Finding the largest number in the list through manual comparison

Here, we are going to find the largest number in the list manually. Here, we are first going to make some assumptions, and then manually carry out the comparison, to find the largest number in the list.

So, let’s have a look at the steps that we are going to follow here –

let’s say that we already have the list(and that too non-empty). After that –

  • First of all, we are going to consider that the first element in the list is the largest number. This is just an assumption, so it could be wrong, or right.
  • After that, we are going to loop through the elements of the list, and check if the current element from the list, is greater than the assumed largest element. If the condition becomes True, then we need to update our largest element.
  • After we are out of the loop, we get the largest element in the list.
  • We just show the largest number as the output of our program.

So, following the above steps, we are going to perform a simple python program. So, let’s have a look at the program now –

numbers = [34, 23, 65, 6, 76,45, 34]
# We are assuming that the largest number in the list is the first element.
largest = numbers[0]
# Loop through the elements, to find the largest element in the list.
for number in numbers:
    if number>largest:
        largest = number

print(f”The largest number in the list is:{largest}”)

As you can see in the above program, we have a list, and then we are just assuming that the first element in the list is the largest number in the list. After that, we are looping through the elements, finding the largest number in the list. We are comparing if the current element in the list is larger than the largest. If the condition is True, then we update the assumed largest value.

Let’s have a look at the output of the above program –

Output –

The largest number on the list is:76

As you can see, we are getting the largest number in the list as our output. You can also try to execute the above program, and you can try to change the numbers in the list, and observe different outputs.

Conclusion

We have discussed some different approaches to finding the largest number in the list. There are many ways to achieve this goal, but here, we have discussed some simple approaches to finding the largest number in the list. You can also try to perform the above programs, and you can try many other python programs.

FAQ About finding the largest number in the list in python

Q: What is a simple approach to finding the largest number in the list in Python?

Ans: To get the largest number in the list, we can simply make use of the max function. The max function returns the item with the highest value.

Q: How to sort the list in Python?

Ans: To sort the list, we can simply use the sort method.

Q: How to check if the list is empty?

Ans: To check if the list is empty, we just need to check the length of the list. If the length of the list comes out to be zero, then we can say that the list is empty.