Find the Largest Number in a List using Python

find-largest-element-in-list
Liked the Article? Share it on Social media!

When it comes to finding the largest number in a list, we have to iterate or loop through the list and print out the largest value. Below are some methods you can use to find the largest number in a list using Python. If you’re unfamiliar with list in Python follow this guide to get the basic understanding.

  1. Sort the list in ascending order and get the last element of the list.
  2. Sort the list in descending order and get the first element of the list.
  3. Use the built-in python method called ‘max()’ function.
  4. Not using any predefined methods or functions of the python library.

Sort the list in ascending order and get the last element of the list

Input data : [33, 55, 12, 56, 93, 46]

Below is the Python code snippet for above input data.

# list of numbers 
numbersList = [33, 55, 12, 56, 93, 46]

# sorting the list 
numbersList.sort()

# printing the last element
print("Largest element is: ", numbersList[-1])

Here is the result of the above code snippet. I’m using windows command line to execute this code. Above code will give us the maximum number of the numbersList.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
Largest element is: 93

Sort the list in descending order and get the first element of the list

Input data : [33, 55, 12, 56, 93, 46]

Below is the Python code snippet for above input data. But this time we use descending order for finding the maximum number.

# list of numbers
numbersList = [33, 55, 12, 56, 93, 46]

# sorting the list in descending order
numbersList.sort(reverse = True)

# printing the first element
print("Largest element is:", numbersList[0])

Output of above code snippet is below.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
Largest element is: 93

Use the built-in python “max()” function to find the Largest number

Well, it turns out to be we won’t need to re-invent the wheel again. Python library already has a function which we can use for our needs. It is max() function. max() function returns the largest value of input data, in our case its a list. Iterable input value could be “string“, “list“, or “tuple” etc.

# list of numbers
numbersList = [33, 55, 12, 56, 93, 46]

# pass the list to max function
maxValue = max(numbersList)

# printing the first element
print("Largest element is:", maxValue)

Output of above code snippet is below.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
Largest element is: 93

Without using any predefined methods to find the largest number

Out of many common beginner-level interview questions,  an interviewer might tell you to find the largest value in a list without using any predefined method or any sorting functions. Well, how do you achieve that?

# list of numbers
numbers = [33, 55, 12, 56, 93, 46]

# initial variable, so that we can compare two numbers
largestSoFar = 0

for theNum in numbers :
    if theNum > largestSoFar :
        largestSoFar = theNum
		
print("Largest element is:", largestSoFar)

Output of above code snippet is below.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
Largest element is: 93

Now let’s go another step. How about asking input from the user rather than hard-coding values by ourselves. In this case, I’m using the windows command line for providing input from the user. You can use terminals in both Mac or Linux operating systems. After executing this program you’ll get the largest number from the list which users have provided.

Let’s build this little program 🤗

Steps I’m going to use in this python script.

  • For reading user input, I’m using input() built-in function.
  • I wrap whole input function inside int() function. Because I want to get integer numbers, not floating numbers.
  • I’m going to initialize an empty list and called it a list.
  • For error handling, I’m going to use try and except, because we cannot guarantee that the user will enter the correct data type that the program expects. 
  • I’m using for loop to iterate over the number that I have provided in the input function.
  • When iterating in each number, I’m going to append it into my initialized empty list.
  • After all that finish, I am using good old max() function to get the maximum number from my list.
  • If the user not going to provide any number in the input function our program going to exit.

That’s it. Here is our little program yet powerful.

list = []

try :
    how_many_elements  = int(input("How Many Numbers ? : "))
	
    for n in range(how_many_elements) :
        number = int(input("Enter number and hit ENTER: "))
        list.append(number)
		
    maxVal = max(list)
    print("Largest element is: ", maxVal)
	
except :
    print("Did not found any number")

Output of above code snippet is below.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
How Many Numbers ? : 6
Enter number and hit ENTER: 33
Enter number and hit ENTER: 55
Enter number and hit ENTER: 12
Enter number and hit ENTER: 56
Enter number and hit ENTER: 93
Enter number and hit ENTER: 46
Largest element is:  93

How about the user provides string or text value rather than a number. Then according to our code, the program going to terminate.

C:\Users\devzigma\Documents\python_tuts> python find_largest_number.py
How Many Numbers ? : text
Did not found any number
C:\Users\devzigma\Documents\python_tuts>

Conclusion

In this tutorial, we have learned how to find the largest number in a given list using Python. These same concepts apply to find the small number in a list as well. You can play around with this code. Break it, make it work.


Liked the Article? Share it on Social media!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top