The max() function in Python

In this Python tutorial, we will see what is max() function in Python and How we can apply it to different kinds of data-types in Python with demonstrative examples.

What is max() function in Python

In the Python programming language, the max() function is a built-in function that is used to find the largest item between two or more parameters. This function can be applied to a range of different Data types, such as numbers, strings, lists, tuples, and among others.

Syntax and Parameters:

The syntax of the max() function in Python is as follows:

max(arg1, arg2, *args[, key])

Here, Parameters:

  • arg1, arg2, *args are the objects to be compared. They can be any iterable like lists, tuples, etc.
  • key (optional) is a function where comparison of iterable is performed based on its return value.

Application of max() function in Python

The max() function can be applied to many Data types in Python. They can be:

  • Numbers
  • Strings
  • Lists or Nested lists
  • Tuples
  • Sets
  • Dictionaries

We will see how the max() function works with all of these Data types in Python one by one, with examples.

Application of the max() function with numbers in Python

The simplest use of the max() function is comparing numbers in Python.

Let’s just take some random numbers and find the maximum value between them.

# numbers = 60,37,5,64,63,16

# printing the max of these numbers

print(max(60, 37, 5, 64, 63, 16))

The Output is:

64
max() function in Python with random numbers

Another way to compare numbers with the max() function in Python is:

Let’s imagine two professional basketball teams, the Los Angeles Lakers and the Boston Celtics, competing in a game. If the Lakers score 105 points and the Celtics score 98 points, we could use the Python max() function to determine the winning team:

Lakers_score = 105
Celtics_score = 98

max_score = max(Lakers_score, Celtics_score)

print("The highest score is:", max_score)

The output is: This program will indicate that the Lakers won the game.

The highest score is: 105
max() function in Python with numbers

Note: Numbers can be an Integer or Float.

This way we can apply the max() function in Python with Numbers.

Application of the max() function with strings in Python

For strings, Python’s max() function compares the strings lexicographically based on the ASCII values of the characters.

For example: If we compare the two cities “New York” and “Los Angeles”, the function will return “New York” because the ASCII value of ‘Y’ in “New York” is higher than the ASCII value of ‘A’ in “Los Angeles”.

city1 = "Los Angeles"
city2 = "New York"

print("The city that comes last lexicographically is:", max(city1, city2))

The Output is:

The city that comes last lexicographically is: New York
max() function in Python on strings

This way we can apply the max() function in Python with strings.

Application of max() function on lists in Python

The max() function is particularly useful when working on lists in Python.

For instance, let’s imagine a Python list that represents the populations (in millions) of the ten most populous cities in the USA:

population_millions = [8.4, 4.0, 2.7, 1.7, 1.4, 0.9, 0.7, 0.7, 0.7, 0.6]

max_population = max(population_millions)

print("The highest population (in millions) is:", max_population)

The output is:

The highest population (in millions) is: 8.4

This program indicates that New York City, with a population of 8.4 million, is the most populous city on the list.

max() function in Python on lists

Let’s try to apply the max() function on Nested Lists in Python:

Consider a more complex example. Suppose we’re tracking temperatures in five major U.S. cities over a week(in F). This data could be stored in a nested Python list, where each sublist represents a city and contains daily temperatures:

temperatures = [
    [67, 70, 72, 68, 71, 69, 71],
    [75, 77, 78, 76, 78, 77, 77],
    [85, 87, 88, 86, 85, 86, 87],
    [60, 62, 63, 61, 60, 62, 63],
    [80, 82, 83, 81, 82, 81, 82],
]

max_temperatures = [max(city_temperatures) for city_temperatures in temperatures]

highest_temperature = max(max_temperatures)

print("The highest temperature of the week across all cities was(in F):", highest_temperature)

The output is:

The highest temperature of the week across all cities was(in F): 88
max() function in Python on nested lists

This way we can apply the max() function on Lists or Nested lists in Python.

Application of max() function to tuples in Python

Just as with lists, the max() function can be applied to tuples in Python. A tuple is an ordered collection of immutable elements. This means that, once a tuple is created, its elements cannot be changed.

Here’s an example where we have a Python tuple that represents the ages of the oldest living persons in the USA:

ages = (115, 114, 113, 112, 111, 110)

oldest_age = max(ages)

print("The oldest living person in the USA is:", oldest_age, "years old.")

The Output is:

The oldest living person in the USA is: 115 years old.
max() function in Python on tuples

This way we can apply the max() function to Tuples in Python.

Application of max() function on sets in Python

The max() function can also be used on sets in Python. A set in Python is an unordered collection of unique elements. Like lists and tuples, we can find the maximum value in a set using the max() function.

Here’s an example where we have a Python set of the top 5 highest mountains in the USA by elevation (in feet):

mountain_elevations = {20310, 18008, 17400, 16873, 16239}

max_elevation = max(mountain_elevations)

print("The highest mountain in the USA is:", max_elevation, "feet high.")

The output is:

The highest mountain in the USA is: 20310 feet high.

In this case, the program would output will indicate that Denali (Mount McKinley) in Alaska, with an elevation of 20,310 feet, is the highest peak in the USA.

max() function in Python on sets

This way we can apply the max() function on sets in Python.

Application of max() function on dictionaries in Python

We can also use the max() function on dictionaries in Python. However, dictionaries behave differently compared to lists, sets, and tuples when used with the max() function, because dictionaries are made up of key-value pairs.

If we call max() function on a Python dictionary without any additional parameters, it will return the maximum key, not the maximum value.

Here’s an example. Suppose we have a Python dictionary representing the GDP (in trillions) of some countries:

gdp = {
    "USA": 21.43,
    "China": 15.42,
    "Japan": 4.97,
    "Germany": 3.71,
    "India": 2.94
}

max_gdp_country = max(gdp)

print("The country with the largest key is:", max_gdp_country)

The output is:

The country with the largest key is: USA
max() function in Python on dictionary keys

This happens not because the USA has the highest GDP, but because “USA” is the highest key lexicographically.

If we want to find the country with the maximum GDP value, we need to use the Python max() function with a key parameter. Here, gdp.get is a Python function that, for a given key, returns its corresponding value. So max(gdp, key=gdp.get) will return the key that has the maximum value.

gdp = {
    "USA": 21.43,
    "China": 15.42,
    "Japan": 4.97,
    "Germany": 3.71,
    "India": 2.94
}

max_gdp_country = max(gdp, key=gdp.get)

print("The country with the largest GDP is:", max_gdp_country)

The output is:

The country with the largest GDP is: USA
max() function in Python on dictionary with key parameter

This way we can use the max() function in Python on dictionaries.

Conclusion:

The max() function in Python offers an easy way to find the maximum element from a group of values or a sequence. Python’s max() function can make our task significantly easier. As with any tool, the key to using the max() function effectively is understanding how it works with different Data types in Python like Numbers, strings, lists, tuples, sets, and dictionaries.

You may also like to read: