How to find the maximum value in the Python list? You can find the maximum value in a list using various functions of Python. A list is a data structure that allows you to store and manipulate a collection of elements. Each element in the list has an index associated with it, starting from 0 for the first element. You can take multiple approaches to find the maximum value or the largest number in a list. Here, I will explain a few different ways to use built-in functions and custom code.
For example, by using the max(), sort(), sorted(), reduce(), heap queue, brute force approach, and tail recursive algorithm. In this article, I will explain how to find the maximum value in the list by using all these functions with examples.
1. Quick Examples of Finding Maximum Value in the List
If you are in a hurry, below are some quick examples of how to get the maximum value in the list.
# Quick examples of finding maximum value in the list
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
# Example 1: Using max() function
# Get the maximum value in the list
max_value = max(mylist)
# Example 2: Find the maximum value
# Using sort() function
mylist.sort()
max_value = mylist[-1]
# Example 3: Using sort() function
mylist.sort(reverse=True)
max_value = mylist[0]
# Example 4: Find the maximum value
# Using sorted() function
sorted_list = sorted(mylist)
max_value = sorted_list[-1]
# Example 5: Using sorted() function with reverse
# param & get the maximum value
sorted_list = sorted(mylist, reverse=True)
max_value = sorted_list[0]
# Example 6: Get maximum value
# Using reduce() function
max_value = reduce(max, mylist)
# Example 7: Using reduce() function along with lambda
max_value = reduce(lambda x, y: x if x > y else y, mylist)
# Example 8: Find the maximum value
# Using heap queue
heapq.heapify(mylist)
# Example 9: Using brute force approach
max_value = float(‘-inf’)
for num in mylist:
if num > max_value:
max_value = num
# Example 10: Using tail recursive algorithm
mylist = [5, 17, 32, 14, 10, 21]
def func(arr, max_=None):
if(max_ is None):
max_ = arr.pop()
current = arr.pop()
if(current > max_):
max_ = current
if(arr):
return func(arr, max_)
return max_
result = func(mylist)
2. Find the Maximum Value of List Using max() Function
You can use the max() function to find the maximum value in a list. It takes an iterable(such as list, string, tuple, or, set) as its argument and returns the largest element from that iterable. For example, first, initialize a list called mylist, then use the max() function to find the maximum value in the list.
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Using max() function
# Get the maximum value in the list
max_value = max(mylist)
print(“The largest number is:”, max_value)
Yields below output.
As you can see from the above largest value 32 has been returned.
You can also find the largest string in the list using max() function, that will compare the strings based on their lexicographical order (alphabetical order). For instance, the max() function is used to find the largest string in the list mylist. The strings are compared alphabetically, and the largest string is determined to be ‘Python‘.
# Initialize list
mylist = [“Python”,”C++”,”Java”,”Hadoop”]
print(“Original list: “, mylist)
# Using max() to find the max value
max_value = max(mylist)
print(“The largest string is:”, max_value)
Yields below output.
3. Find Maximum Value in List Using sort() Function
You can also find the maximum value in the list using the sort() function. For instance, initialize a list called mylist with some values, then use the sort() function to sort the list in ascending order. Finally, you can get the maximum value from the sorted list by using [] notation.
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Find maximum value
# Using sort() function
mylist.sort()
max_value = mylist[-1]
print(“The largest number is:”, max_value)
# Output:
# Original list: [5, 17, 32, 14, 10, 21]
# The largest number is: 32
Similarly, you can use the sort() function with reverse param to find the maximum value in a list. The sort() function is used to sort the list in ascending order/descending order. Set the reverse=True argument and pass it into sort(), it will sort the list in descending order. After sorting, the first element in the sorted list is a maximum element, which is accessed using indexing (mylist[0]).
# Using sort() function with param reverse
# get the max value in a list
mylist.sort(reverse=True)
max_value = mylist[0]
print(“The largest number is:”, max_value)
Yields the same output as above.
4. Find the Maximum Value in the List Using sorted() Function
You can also use the sorted() function to find the maximum value in a list. For example, First, initialize a list mylist with some values, then use the sorted() function to sort the list in ascending order. Finally, by retrieving the last element from the sorted list using [] notation, you can get the maximum value from a list.
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Find the maximum value
# Using sorted() function
sorted_list = sorted(mylist)
max_value = sorted_list[-1]
print(“The largest number is:”, max_value)
In another way, you can find a maximum value in a list using the sorted() function with reverse parm. The sorted() function is used to create a new sorted list in descending order. Set the reverse=True argument and pass it into the sorted() function, it will sort the list in descending order. After sorting, the maximum value is the first element in the sorted list, which is accessed using indexing sorted_list[0].
# Using sorted() function
sorted_list = sorted(mylist, reverse=True)
max_value = sorted_list[0]
print(“The largest number is:”, max_value)
Yields the same output as above.
5. Find the Maximum Value in the List Using reduce() Function
You can also find the maximum value from the list using the reduce(), which is a very useful function in functional programming and can also be used in Python. However, as you mentioned, it has been moved to the functools module.
In the below example, First, initialize a list mylist with some values. Use the reduce() function to find the maximum value in the list by passing the max function as the first argument and the mylist as the second argument. Assign the result to a variable called max_value.
from functools import reduce
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Get maximum value
# Using reduce() function
max_value = reduce(max, mylist)
print(“The largest number is:”, max_value)
Alternatively, you can use the reduce() function along with the lambda function to find the maximum value from a list of numbers in Python. The reduce() function takes two arguments one is a lambda function and the other one is the given list.
The lambda function compares two numbers x and y and returns the larger of the two. The reduce() function applies this lambda function successively to the elements of the list until it reduces the list to a single value, which is the maximum value. Finally, you print “The largest number is: 32” to the console.
# Using reduce() function along with lambda
# get maximum value
max_value = reduce(lambda x, y: x if x > y else y, mylist)
print(“The largest number is:”, max_value)
Yields the same output as above.
6. Find the Maximum Value in the List Using Heap Queue
You can also use the heapq module to implement a heap queue, which is a binary heap data structure that allows efficient access to the maximum or minimum element of the collection.
In the below example, you created a list of mylist and then used the heapq.heapify() function to convert the list into a heap. Then, you used the heapq.nlargest() function to get the largest element from the heap, which is 32.
import heapq
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Find the maximum value
# Using heap queue
heapq.heapify(mylist)
max_value = heapq.nlargest(1, mylist)[0]
print(“The largest number is:”, max_value)
Yields the same output as above.
7. Find the Maximum Value in the List Using Brute Force Approach
You can also find the maximum value in a list using a brute force approach, you can iterate over the list and compare each element with a variable that keeps track of the current maximum value.
In the below example, you initialize the max_value variable to negative infinity, which acts as a starting point. Then, you iterate over each element in the list mylist. If an element is greater than the current max_value, you update the max_value to that element. After iterating through all the elements, the max_value variable will hold the maximum value in the list.
# Initialize list
mylist = [5, 17, 32, 14, 10, 21]
print(“Original list: “, mylist)
# Using brute force approach
max_value = float(‘-inf’)
for num in mylist:
if num > max_value:
max_value = num
print(“The largest number is:”, max_value)
Yields the same output as above.
8. Using Tail Recursive Algorithm
You can also use the tail-recursive algorithm to find the maximum value in a list. The func() function takes two arguments: arr, which represents the list of numbers, and max_, which represents the maximum value found so far. The function starts by checking if the max_ is None. If it is, the function initializes max_ with the last element of the list using the pop() function. Next, the function pops the next element from the list and checks if it is greater than the current max_. If it is, the max_ is updated to the new value.
# Using tail recursive algorithm
mylist = [5, 17, 32, 14, 10, 21]
def func(arr, max_=None):
if(max_ is None):
max_ = arr.pop()
current = arr.pop()
if(current > max_):
max_ = current
if(arr):
return func(arr, max_)
return max_
result = func(mylist)
print(result)
Conclusion
In this article, I have explained how to find the maximum value in the list in Python by using the max(), sort(), sorted(), reduce(), lambda, heap queue, brute force approach, and tail recursive algorithm with examples.
Happy Learning !!
How to find the maximum value in the Python list? You can find the maximum value in a list using various functions of Python. A list is a data structure that allows you to store and manipulate a collection of elements. Each element in the list has an index associated with it, starting from 0 Read More Python, Python Tutorial, Pending review by admin, Python List Examples