Program to Find the Largest Number in a List

Python Program to Find the Largest Number in a List

Lists in Python:

Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable.

Given a list, the task is to find the largest number in the given list in Python.

Examples:

Example1:

Input:

given list =[1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82, 82, 189, 274, 284, 467, 677,
                   871, 873, 876, 947, 1838, 2954, 5123]

Output:

The largest element present in the given list [1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82,
82, 189, 274, 284, 467, 677, 871, 873, 876, 947, 1838, 2954, 5123] = 5123

Example2:

Input:

given list =[1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34]

Output:

The largest element present in the given list [1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34] = 34

Program to Find the Largest Number in a List in Python

There are several ways to find the largest element/item in the given list some of them are:

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Method #1:Using sort() function in Ascending Order(Static Input)

Approach:

  • Given the list as static input and store it in a variable.
  • Sort the given list using the sort() function(which sorts the elements in ascending order).
  • Calculate the length of the given list using the len() function and store it in a variable.
  • Print the largest element in the given list with the help of the length of the list variable(The last element can be found at the index length of list -1)
  • The Exit of the Program.

Below is the implementation:

# Given the list as static input and store it in a variable.
gvnList = [12, 34, 11, 9, 2, 4, 1, 23, 28,
           29, 25, 23, 31, 18, 15, 8, 13, 15, 17]
# Sort the given list using the sort() function
# (which sorts the elements in ascending order).
gvnList.sort()
# Calculate the length of the given list using the len() function
# and store it in a variable.
lstLeng = len(gvnList)
# Print the largest element in the given list with the help of the length of the
# list variable(The last element can be found at the index length of list -1)
largstele = gvnList[lstLeng-1]
print('The largest element present in the given list', gvnList, '=', largstele)

Output:

The largest element present in the given list [1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34] = 34

Method #2:Using sort() function in Ascending Order(User Input)

Approach:

  • Given the list as user input separated by spaces using map(),split(),list() and int functions.
  • Store the list in one variable.
  • Sort the given list using the sort() function(which sorts the elements in ascending order).
  • Calculate the length of the given list using the len() function and store it in a variable.
  • Print the largest element in the given list with the help of the length of the list variable(The last element can be found at the index length of list -1)
  • The Exit of the Program.

Below is the implementation:

# Given the list as user input separated by spaces
# using map(),split(),list() and int functions.
# Store the list in one variable.
gvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))
# Sort the given list using the sort() function
# (which sorts the elements in ascending order).
gvnList.sort()
# Calculate the length of the given list using the len() function
# and store it in a variable.
lstLeng = len(gvnList)
# Print the largest element in the given list with the help of the length of the
# list variable(The last element can be found at the index length of list -1)
largstele = gvnList[lstLeng-1]
print('The largest element present in the given list', gvnList, '=', largstele)

Output:

Enter some random elements to the list separated by spaces = 36 18 1 34 9 7 64 15 284 1838 189 873 52 47 18 
65 677 876 2954 26 82 871 467 274 5123 82 947 27 47 18 1 4 3 5 2
The largest element present in the given list [1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82,
82, 189, 274, 284, 467,  677, 871, 873, 876, 947, 1838, 2954, 5123] = 5123

Method #3:Using sort() function in Descending Order(User Input)

Approach:

  • Given the list as user input separated by spaces using map(),split(),list() and int functions.
  • Store the list in one variable.
  • Sort the given list in descending order using the sort() and reverse=True(which sorts the elements in descending order).
  • Print the largest element in the given list by printing the first element in the given list(which is having an index of 0)
  • The Exit of the Program.

Below is the implementation:

# Given the list as user input separated by spaces
# using map(),split(),list() and int functions.
# Store the list in one variable.
gvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))
#Sort the given list in descending order using the sort() 
#and reverse=True(which sorts the elements in descending order).
gvnList.sort(reverse=True)

#Print the largest element in the given list by printing the first
#element in the given list(which is having an index of 0)
largstele = gvnList[0]
print('The largest element present in the given list', gvnList, '=', largstele)

Output:

Enter some random elements to the list separated by spaces = 36 282 194 196 578 1 2 34 727 84
The largest element present in the given list [727, 578, 282, 196, 194, 84, 36, 34, 2, 1] = 727

Method #4:Using max() function(Static Input)

This is the easiest and fastest approach.

Approach:

  • Given the list as static input and store it in a variable.
  • Print the maximum element in the given by providing the given list as an argument to max() function.
  • The Exit of the Program.

Below is the implementation:

# Given the list as static input and store it in a variable.
gvnList = [49, 189, 18, 9, 10, 2, 49, 1, 38, 29]
# Print the maximum element in the given by
# providing the given list as an argument to max() function.
largstele = max(gvnList)
print('The largest element present in the given list', gvnList, '=', largstele)

Output:

The largest element present in the given list [49, 189, 18, 9, 10, 2, 49, 1, 38, 29] = 189

This is the efficient way to find the largest element in the given list.

Method #5:Using max() function(User Input)

This is the easiest and fastest approach.

Approach:

  • Given the list as user input separated by spaces using map(),split(),list() and int functions.
  • Store the list in one variable.
  • Print the maximum element in the given by providing the given list as an argument to max() function.
  • The Exit of the Program.

Below is the implementation:

# Given the list as user input separated by spaces
# using map(),split(),list() and int functions.
# Store the list in one variable.
gvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))
# Print the maximum element in the given by
# providing the given list as an argument to max() function.
largstele = max(gvnList)
print('The largest element present in the given list', gvnList, '=', largstele)

Output:

Enter some random elements to the list separated by spaces = 46 28 1 937 472 8545 28 1 83 94 28 95 27 58
The largest element present in the given list [46, 28, 1, 937, 472, 8545, 28, 1, 83, 94, 28, 95, 27, 58] = 8545

Related Programs: