Get the largest value from a List in Python
Python is a versatile language that you can use to write programs of all kinds. In this article, you'll learn how to get the largest value from a list in Python. You'll also learn how to use the built-in max() function to get the largest value from a list.
numbers = [30, 50, 90, 10, 70, 20]
max_val = max(numbers)
print("Maximun value is: ", max_val)
Below are the methods that can be used to get the largest value from a List using Python.
Solution 1: Get the largest value from a List using max() function
Python has a built-in function called max() that allows you to get the largest value from a list. To use this function, you simply pass in the list as an argument. The max() function will then return the largest value in the list.
Syntax
max(List)
Code example
numbers = [5, 9, 4, 7, 10, 3]
max_val = max(numbers)
print("Maximun value is: ", max_val)
Output
Maximun value is: 10
- The above code will create a list called "numbers" and store the values 5, 9, 4, 7, 10, and 3 in that list.
- Then find the maximum value in the list using max() function and store it in a variable called "max_val".
- Finally, print out the value stored in "max_val".
Solution 2: Using sort() function
When working with lists in Python, it is often useful to get the largest or smallest value in the list. We can do this using the sort() function.
The sort() function takes a list and returns a new list with the same values in sorted order. The sorted order is determined by the order of the elements in the list.
The largest value in a list is the last element in the sorted list. The smallest value in a list is the first element in the sorted list.
To get the largest value from a list, use the sort() function to sort the list in reverse order. The reverse order is specified by passing the reverse=True argument to the sort() function.
The largest value in the list will be the last element in the sorted list. To get the smallest value from a list, use the sort() function to sort the list in ascending order. The ascending order is the default order for the sort() function.
The smallest value in the list will be the first element in the sorted list.
# create a list
numbers = [5, 9, 4, 7, 10, 3]
print("Original List is: ", numbers)
# sort the list
numbers.sort()
# get the last value from the list
max_val = numbers[-1]
print("Maximun value is: ", max_val)
Output
Original List is: [5, 9, 4, 7, 10, 3]
Maximun value is: 10
The code above creates a list of numbers and prints it to the console. It then uses the List.sort() method to sort the list in ascending order. Finally, it gets the last value from the list using list indexing and prints it to the console.
Solution 3: Using For loop
When we want to find the largest value from a list, we can use a for loop in python. This is because a for loop will iterate through the list and compare the values to find the largest one.
# create a list
numbers = [5, 9, 4, 7, 10, 3]
print("Original List is: ", numbers)
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
print("Largest value is: ", max_val)
Output
Original List is: [5, 9, 4, 7, 10, 3]
Largest value is: 10
The code above creates a list of numbers, then assigns the value of the first number in the list to the max_val variable. It then loops through each number in the list and checks if it is greater than the max_val. If it is, it assigns that value to max_val. Finally, it prints the largest value in the list.
- Get the second largest number from a list in Python
- Find the largest number in a list Python
- Get last item or value from a List in python
- Remove items less than a specific value from Python list
- Find minimum value from a List in Python
- Get tofixed of a value using round() Python
- Get Key from a Dictionary using Value in Python