Table of contents
1.
Introduction
2.
max() Function with Objects
2.1.
Syntax
2.2.
Parameters
2.3.
Return
3.
Examples
3.1.
Example 1: Finding the Maximum Value in a List
3.2.
Python
3.3.
Example 2: Finding the Maximum of Multiple Arguments
3.4.
Python
3.5.
Example 3: Using the key Parameter with Custom Objects
3.6.
Python
4.
max() Function with Iterable in Python
4.1.
Syntax
4.2.
Parameters
4.3.
Return
5.
Examples
5.1.
Example 1: Finding the Maximum Value in a List
5.2.
Python
5.3.
Example 2: Finding the Maximum Value with a Key Function
5.4.
Python
5.5.
Example 3: Using default with an Empty Iterable
5.6.
Python
6.
Frequently Asked Questions
6.1.
Can max() handle both numbers and strings?
6.2.
How do I find the maximum value based on a specific attribute?
6.3.
What happens if max() is used on an empty iterable without a default value?
6.4.
Can max() work with a list of dictionaries?
7.
Conclusion
Last Updated: Aug 22, 2025
Easy

Python max() Function

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, the max() function is used to find the largest item in an iterable or among two or more arguments. This function is handy when you need to determine the maximum value in a list, tuple, or any other iterable. 

Python max() Function

It can also be used with custom objects to find the maximum based on specific attributes.

max() Function with Objects

Syntax

max(arg1, arg2, *args, key=None)

Parameters

  • arg1, arg2, *args: These are the values or objects to compare. You can pass two or more arguments.
     
  • key: This is an optional function that takes a single argument and returns a value for comparison. If not provided, max() uses the default comparison.

Return

The max() function returns the largest value among the provided arguments or within the iterable.

Examples

Example 1: Finding the Maximum Value in a List

  • Python

Python

numbers = [1, 3, 5, 2, 8]

max_value = max(numbers)

print(max_value)
You can also try this code with Online Python Compiler
Run Code


Output

8


In this example, max() finds the largest number in the list.

Example 2: Finding the Maximum of Multiple Arguments

  • Python

Python

max_value = max(4, 7, 1, 9)

print(max_value)
You can also try this code with Online Python Compiler
Run Code


Output

9


Here, max() compares the numbers directly and returns the largest one.

Example 3: Using the key Parameter with Custom Objects

Consider a list of dictionaries where each dictionary represents a person with name and age:

  • Python

Python

people = [

   {'name': 'Alice', 'age': 25},

   {'name': 'Bob', 'age': 30},

   {'name': 'Charlie', 'age': 22}

]

oldest = max(people, key=lambda person: person['age'])

print(oldest)
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'Bob', 'age': 30}


Here, max() finds the dictionary with the highest age value.

max() Function with Iterable in Python

Syntax

max(iterable, key=None, default=None)

Parameters

  • iterable: This is the iterable (like a list, tuple, or set) from which to find the maximum value.
     
  • key: This is an optional function to compute a comparison key from each item.
     
  • default: This is an optional value to return if the iterable is empty. If not provided and the iterable is empty, max() raises a ValueError.

Return

The max() function returns the largest item in the iterable. If the iterable is empty and no default is provided, it raises a ValueError.

Examples

Example 1: Finding the Maximum Value in a List

  • Python

Python

numbers = [10, 20, 4, 45, 99]

max_value = max(numbers)

print(max_value)
You can also try this code with Online Python Compiler
Run Code


Output

99


Here, max() returns the highest number from the list.

Example 2: Finding the Maximum Value with a Key Function

Suppose we have a list of tuples where each tuple represents a person’s name and score:

  • Python

Python

scores = [('Alice', 85), ('Bob', 90), ('Charlie', 78)]

highest_score = max(scores, key=lambda x: x[1])

print(highest_score)
You can also try this code with Online Python Compiler
Run Code


Output

('Bob', 90)


In this example, max() uses the key parameter to find the tuple with the highest score.

Example 3: Using default with an Empty Iterable

  • Python

Python

empty_list = []

max_value = max(empty_list, default='No values')

print(max_value)
You can also try this code with Online Python Compiler
Run Code


Output

No values


When the list is empty, max() returns the default value specified.

Frequently Asked Questions

Can max() handle both numbers and strings?

Yes, max() can handle numbers, strings, and other data types, as long as they are comparable.

How do I find the maximum value based on a specific attribute?

Use the key parameter with a lambda function or another function that extracts the attribute for comparison.

What happens if max() is used on an empty iterable without a default value?

max() raises a ValueError. Provide a default value to avoid this issue.

Can max() work with a list of dictionaries?

Yes, use the key parameter to specify which dictionary field to use for comparison.

Conclusion

The max() function in Python is a powerful tool for finding the largest item in a collection of values or objects. Whether you're working with simple numbers or more complex data structures, max() provides flexibility with its key and default parameters. Understanding how to use max() effectively can help streamline your data analysis and decision-making processes.

Recommended Readings:

Live masterclass