What is Max () in Python?

Max is another built-in Python function that might be beneficial for evaluating data (). It operates similarly to min(), which we just discussed, but in reverse. Instead of looking for the smallest object, it looks for the largest item.

Like min(), it accepts either a sequence or a series of two or more items as input. Max() returns the biggest item in a series when given a sequence. When given a list of two or more items, max() returns the biggest item in the list.

Max () Syntax

The method’s syntax is as follows:


  • An iterable or one or more objects — if an iterable is given as a parameter, the iterable’s maximum value will be returned. Otherwise, the highest value among the items will be returned.
  • key – a function that accepts one parameter and returns a result that defines the semantics of key extraction.
  • default – an object to return if the iterable is empty. Python will throw a ValueError if the default value is not given and an empty iterator is passed.

There are a few things to keep in mind while using the Python Max() method.

  • If there is just one parameter, it should be an iterable such as a string, list, tuple, or similar. The iterable’s biggest item is returned.
  • If two or more arguments are given, the biggest one will be returned.
  • We may define a key argument function that will be utilized to find the biggest item. This non-mandatory parameter is mainly used when the arguments are custom objects.
  • If the specified iterable is empty, the default argument defines an object to return. If the iterable is empty and no default is set, a ValueError exception is thrown.
  • If more than one most significant element is discovered, the first one is returned.

Python Max() Function Examples

Example 1: Get the largest value using the max() function.

This is the most basic and uncomplicated method for determining the most significant element. The max() method in Python returns the biggest item in an iterable. It may also be used to calculate the maximum value of two or more parameters.

The following example employs an input list and provides it as an argument to the max function.

The output will be

If the list elements are strings, they are first arranged alphabetically, and then the longest string is returned.

The output will now be

Example 2: Using the Brute Force Approach, find the maximum value.

This is the simplest solution. However, it is somewhat slower than the max() method since we utilize it in a loop.

The output for this will be given as

The above example defines a function large() to discover the greatest value, which accepts the input list as an argument. In this method, the list’s first member is initially stored in a variable.

Each element in the for loop is compared to this root element variable. If the element is bigger than the root element, we give the value of this item to the root element variable, and finally, we get the largest element after comparing.

Example 3: max() in conjunction with a key function

As previously stated, the key is a one-line ordering function used to find the largest value among a group of values.

For example, suppose we wish to identify the tuple with the highest value of the second member from a list of tuples. Let’s see what we can come up with.

The output will be

In this case, the function f() is a user-defined function that returns the second member of the given tuple. Passing this function as a key to the max() method guarantees that a tuple with the biggest 2nd member is returned. In our case, it is (6, 8, 4).

Multiple Iterables Passed as Arguments

The Python max() function can return the biggest of many iterable items passed as arguments. These parameters can be iterable, such as string, character, tuple, list, etc.

For lists, tuples, and so on, the max() function returns the object with the maximum 0th member by default. And for strings, it compares the first character of each string provided.

Below we have picked an example for tuples. Look at the code carefully.

The output will be

For this example, three tuples with specific beginning values have been immediately supplied to the max() function. This returns the tuple with the greatest initial member, i.e., (7, 37, 1).

Python max() Function Summary

The Python max() method returns the item with the highest value or the item with the greatest value in an iterable. If the values are strings, an alphabetical comparison is made. The syntax for many items is max(n1, n2, n3, …) or max(iterable) in the case of an iterable.

Please note that if the default value is not defined and an empty iterable is supplied as arguments to the max() method produces a ValueError.

Leave a Comment