Skip to content

Python List max() Function Malli Spark By {Examples}

  • by

The max() function is a built-in function in Python that is used to find the maximum element in a list, tuple, or any other iterable object. It takes the iterable as an argument and returns the largest item. In this article, I will explain the Python list max() function and using its syntax, parameters, and usage how we can find the maximum element from a given list with examples.

1. Quick Examples of List max() Function

If you are in a hurry, below are some quick examples of the list max() function.

#  Quick examples of list max() function

# Example 1: Get maximum value
# Using max() function
mylist = [12, 53, 24, 66, 42]
max_value = max(mylist)

# Example 2: Find the tuple maximum value
mytuple = (12, 53, 24, 66, 42)
max_value = max(mytuple)

# Example 3: Find the set maximum value
myset = {12, 53, 24, 66, 42}
max_value = max(myset)

# Example 4: Get maximum value
# Using reduce() function
mylist = [12, 53, 24, 66, 42]
max_value = reduce(max, mylist)

# Example 5: Using reduce() function along with lambda
max_value = reduce(lambda x, y: x if x > y else y, mylist)

# Example 6: Get maximum value
# Using sort() function
mylist = [12, 53, 24, 66, 42]
mylist.sort(reverse=True)
max_value = mylist[0]

# Example 7: Get maximum value
# Using sorted() function
sorted_list = sorted(mylist, reverse=True)
max_value = sorted_list[0]

# Example 8: Get the maximum value
# Using heap queue
mylist = [12, 53, 24, 66, 42]
heapq.heapify(mylist)
max_value = heapq.nlargest(1, mylist)[0]

2. Python List max() Function

list.max() function in Python takes the list as an argument and returns the largest item from the list. Using the max() function we can find the maximum item from other iterable objects such as tuple or set. If the items are string comparison can be done alphabetically.

2.1 Syntax of List max() Function

Following is the syntax of the list max() Function.

# Syntax of max() function
max(list)

2.2 Parameter of list max()

list – This is a list from which we can find out the max element.

2.3 Return Value

It returns the maximum value from a list or an iterable.

3. Get Maximum Value Using max() Function

You can use the max() function in Python to get the maximum value from a list or an iterable. For example, the max() takes the list named mylist as an argument and returns the largest number in the list, which is 66.

# Initialize list
mylist = [12, 53, 24, 66, 42]
print(“Original list: “, mylist)

# Get maximum value
# Using max() Function
max_value = max(mylist)
print(“Get maximum value: “, max_value)

Yields below output.

Note that the max() function can also be used with other iterables, such as tuples or sets, as well as multiple arguments. In all of these examples, the max() function is used to find the maximum value in the given iterable or arguments.

# Initialize tuple
mytuple = (12, 53, 24, 66, 42)
print(“Original list: “, mytuple)

# Find the tuple maximum value
max_value = max(mytuple)
print(“Get maximum value: “, max_value)

# Initialize set
myset = {12, 53, 24, 66, 42}
print(“Original list: “, myset)

# Find the set maximum value
max_value = max(myset)
print(“Get maximum value: “, max_value)

Yields the same output as above.

4. Get Maximum Value Using reduce() Function

You can also get 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, create a list of numbers called mylist. 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 = [12, 53, 24, 66, 42]
print(“Original list: “, mylist)

# Get maximum value using reduce() function
max_value = reduce(max, mylist)
print(“Get maximum value: “, max_value)

You can also use the reduce() function along with the lambda function to get 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 takes two parameters x and y, compares them, and returns the larger of the two. The reduce() function applies this lambda function to each pair of values in the list, iteratively reducing the list until a single value remains, which is the max_value value in the list.

# Using reduce() function along with lambda
max_value = reduce(lambda x, y: x if x > y else y, mylist)
print(“Get maximum value: “, max_value)

Yields the same output as above.

5. Get Maximum Value Using sort() Function

You can also use the sort() function 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]). Finally, the maximum value is printed using the print() function.

# Initialize list
mylist = [12, 53, 24, 66, 42]
print(“Original list: “, mylist)

# Get maximum value
# Using sort() function
mylist.sort(reverse=True)
max_value = mylist[0]
print(“Get maximum value: “, max_value)

Yields the same output as above.

6. Get Maximum Value Using sorted() Function

You can also use the sorted() function to find the maximum value in a list. 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].

# Get maximum value
# Using sorted() function
sorted_list = sorted(mylist, reverse=True)
max_value = sorted_list[0]
print(“Get maximum value: “, max_value)

Yields the same output as above.

7. Get Maximum Value 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 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 84.

import heapq

# Initialize list
mylist = [12, 53, 24, 66, 42]
print(“Original list: “, mylist)

# get the maximum value
# Using heap queue
heapq.heapify(mylist)
max_value = heapq.nlargest(1, mylist)[0]
print(“Get maximum value: “, max_value)

Yields the same output as above.

8. Using Brute Force Approach

You can also find the maximum value using a brute force approach. For example, you first initialize the max_value variable to the first item in the list (mylist[0]). You then loop over each item in the list and compare it with the current maximum value (max_value).

If the current item is greater than max_value, you can update the max_value to the current item. At the end of the loop, you have the maximum value stored in the max_value variable.

# Get Maximum Value
# Using Brute Force Approach
mylist = [12, 53, 24, 66, 42]
print(“Original list: “, mylist)
max_value = mylist[0]

for an item in mylist:
if item > max_value:
max_value = item
print(“Get maximum value: “, max_value)

# Output:
# Original list: [12, 53, 24, 66, 42]
# Get maximum value: 80

9. Conclusion

In this article, I have explained the Python list max() method and using its syntax, parameters, and usage how to find the maximum value from a given list with examples.

Happy Learning !!

 The max() function is a built-in function in Python that is used to find the maximum element in a list, tuple, or any other iterable object. It takes the iterable as an argument and returns the largest item. In this article, I will explain the Python list max() function and using its syntax, parameters, and  Read More Python, Python Tutorial, Pending review by admin, Python List Methods 

Leave a Reply

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