Python program to find largest number in a list

In this article, we will learn to find the largest number in a list using the python program.

We find the largest number in a list using 3 different methods:

  1. Using max() function
  2. By comparing each element
  3. By using sort() function


Using min() function

max() is the in-built function of the python, which returns the largest value of the list.

Explanation

  1. First, we declared the list.
  2. Then, we use the max() function to find the largest number in a list.
  3. Then we print the large value.

Program

1
2
3
4
5
6
7
8
# lsit
list = [11, 10, 13, 15, 28, 9]

# find largest elements using max() function
large = max(list)

# print largest element
print("The largest element of a list is:",large)

Output

The largest element of a list is: 28


By comparing each element

In this method, we will iterate the list and compare each element to find the largest number.

Explanation

  1. First, we declared the list and a large variable.
  2. And, we assume the first element as the largest number and assign them to the large variable.
  3. Then we run the for loop and check each element of the loop is greater than large or not. 
  4. If any element is larger than the large variable then we update the value of the large variable.
  5. Then, lastly, we prin the large variable as the largest number.

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# lsit
list = [11, 10, 13, 15, 28, 9]

# assume the first element is large
large = list[0]

# now iterate the list
for i in range(0,len(list)):
    # check the all elements is greater than large or not
    if list[i] > large:
        # if the elements is larger than large than update the value of large
        large = list[i]
    

# print largest element
print("The largest element of a list is:",large)

Output

The largest element of a list is: 28


Using sort() function

sort() function used to arrange a list in ascending order. so, using the sort() function we will easily find the largest number value from the list.

Explanation

  1. First, we declare the list.
  2. And then we use the sort() function to arrange the list in ascending order.
  3. And then we know that in ascending order the last element is always larger.
  4. So, we print the last element.

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# lsit
list = [11, 10, 13, 15, 28, 9]

# arrange the list in ascending order
list.sort()

# so, the last elements of ascending list is largest
large = list[-1]
    
# print largest element
print("The largest element of a list is:",large)

Output

The largest element of a list is: 28
Next Post Previous Post
No Comment
Add Comment
comment url