Search code snippets, questions, articles...

Find the largest number in a list Python

Have you ever had to find the largest number in a list? Here we will look at a few different methods for doing this. We are doing it using sort() and max() methods.
numbers = [50, 90, 10, 30, 90, 70]

numbers.sort()

largest_number = numbers[-1]

print(largest_number)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

We are getting the largest number in a list using the sort() function here. The sort() method is used to sort the list in ascending and descending order.

In the above code snippet:

1. We are defining a list called numbers that contains multiple integer type values.

2. We are sorting the list using the sort() function in python.

numbers.sort()

3. Getting the last element of the list using python code and storing it to python variable largest_number.

numbers[-1]

4. Printing the largest_number variable.

Sort list and dictionary in Python

Find the largest number using the max() function

We can also get the largest number from a python list using the max() function. It takes the list as a parameter and returns the largest number from this list.

Below is an example to get the largest number from a list in python.

numbers = [50, 90, 10, 30, 90, 70]

largest_num = max(numbers)

print(largest_num)
Was this helpful?
0 Comments