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)
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)
- Get the second largest number from a list in Python
- Find the smallest number in a python list
- Get the largest value from a List in Python
- Find all indexes of an item in a list using python
- Find the most repeated item from a list in Python
- Find minimum value from a List in Python
- Python code to check if a given number exists in a list