Signup/Sign In

Python program to find largest number in a list

In this tutorial, you will learn how to find the largest number in a list. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. Previously, we learned how to find the smallest number in a list, we will be following similar approaches in this tutorial.

For a given list of numbers, the task is to find the largest number in the list.

Input: [10, 3, 20, 9, 11, 15, 23, 6]

Output: 23

Approach to find largest number in a list

For executing this program in Python, there are multiple approaches we can follow-

  1. By comparing each element for finding the largest number
  2. By using max() function
  3. By sorting the list using sort() function and printing the last element in the list

Let us look at each approach in detail.

Approach 1: comparing elements

In this approach, we will run a loop from the starting to the ending of the list and compare each element individually to find the maximum element. We will use for loop in this approach.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function that will find the largest number

Step 2- Declare a variable that will store the largest value

Step 3- Initialise it to the first value in the list

Step 4- Run a loop for all elements in the list

Step 5- Compare each element with the variable which stores the smallest value

Step 6- If the element is larger than the value in the variable

Step 7- Copy the element in the variable

Step 8- Return the variable

Step 9- Declare and initialize list or take input

Step 10- Call function and print output

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

#largest element in list

#function
def largest(list):
    large= list[0]
    for i in list:
        if i>large:
            large=i
    return large

#list
list=[3, 9, 7, 3, 6, 5, 7, 24, 6]
print("largest in ",list,"is")
print(largest(list))


largest in [3, 9, 7, 3, 6, 5, 7, 24, 6] is
24

We have used '\n' for printing the statement in the next line. It is an escape sequence new line character.

Approach 2: max() function

In this approach, we will use max() method which is a built-in function in the Python library. It will return the largest value in the list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Declare a function that will find the largest number

Step 2- Use max() method and store the value returned by it in a variable

Step 3- Return the variable

Step 4- Declare and initialize a list or take input

Step 5- Call the function and print the value returned by it

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach.

#largest element in list

#function
def largest(list):
    large= max(list)
    return large

#input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)

#smallest
print("Largest in ",li,"is")
print(largest(li))            


Enter size of list 6
Enter element of list 4
Enter element of list 8
Enter element of list 9
Enter element of list 2
Enter element of list 3
Enter element of list 1
Largest in [4, 8, 9, 2, 3, 1] is
9

Approach 3: by sorting and negative indexing

In this approach, we will first sort the elements of the list. For sorting the list we will use the predefined sort() function in Python. It will sort the elements in the list in ascending order. We will print the element present at the last position after sorting. This will be the largest element in the list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Declare a function for finding the largest number

Step 2- Use sort() method to sort the list

Step 3- Return the last element in the list after sorting

Step 4- Declare a list and take input or initialize values

Step 5- Call the function

Step 6- Print the value returned by the function

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach

#largest element in list

#function
def largest(list):
    list.sort()
    return list[-1]

#input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)

#smallest
print("largest in ",li,"is")
print(largest(li))            


Enter size of list 6
Enter element of list 4
Enter element of list 9
Enter element of list 6
Enter element of list 1
Enter element of list 10
Enter element of list 33
largest in [4, 9, 6, 1, 10, 33] is
33

To get the element at the last index we have used negative indexing.

Conclusion

So far, we have discussed three ways by which we can find the largest element in the list. We have discussed how to use a for loop for comparing each element in the list and finding the maximum element. We have also used built-in functions like max() and sort() to find the largest element in the list.