Do you want to find the maximum of a Python list? This article gives you everything you need to know to master the **max()** function in Python.

### Description

Python’s built-in **max()** function returns the maximum element of a list or its generalization (an iterable).

### Syntax

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

max(list, default=obj, key=func)

### Arguments

Argument | Description |
---|---|

`list` | The list or iterable from which you get the maximum value. |

`default` | If the iterable is empty, returns this default object. |

`key` | A function that associated a weight to each element in the iterable based on which the maximum is calculated. |

### Return Value

The return value of the `max(list)`

function is a single element from the `list`

that is the maximum of all elements. If the list is empty, the default object is returned, if explicitly defined. If not, a `ValueError`

is thrown.

### Examples

Let’s consider four examples that show how the arguments play together:

**Exercise**: Guess the output of the four print statements in the code. Run the code. How many did you guess right?

## Maximum of List of Lists

*Original article: How to Find the Maximum of a List of Lists?*

To find the maximal list in a list of lists, you need to make two lists comparable. How? With the `key`

argument of the `max()`

function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is returned as the maximum of the list of lists.

**Problem**: Say you have a list of lists (nested list) and you want to find the maximum of this list. It’s not trivial to compare lists—what’s the maximum among lists after all? To define the maximum among the inner lists, you may want to consider different objectives.

- The first element of each inner list.
- The i-th element of each inner list.
- The sum of inner list elements.
- The maximum of inner list elements.
- The minimum of inner list elements.

**Example**: Given list of lists `[[1, 1, 1], [0, 2, 0], [3, 3, -1]]`

. Which is the maximum element?

- The first element of each inner list. The maximum is
`[3, 3, -1]`

. - The i-th element of each inner list (
`i = 2`

). The maximum is`[1, 1, 1]`

. - The sum of inner list elements. The maximum is
`[3, 3, -1]`

. - The maximum of inner list elements. The maximum is
`[3, 3, -1]`

. - The minimum of inner list elements. The maximum is
`[3, 3, -1]`

.

So how do you accomplish this?

**Solution**: Use the `max()`

function with key argument.

Let’s study the solution code for our different versions of calculating the maximum “list” of a list of lists (nested list).

lst = [[1, 1, 1], [0, 2, 0], [3, 3, -1]] # Maximum using first element print(max(lst, key=lambda x: x[0])) # [3, 3, -1] # Maximum using third element print(max(lst, key=lambda x: x[2])) # [1, 1, 1] # Maximum using sum() print(max(lst, key=sum)) # [3, 3, -1] # Maximum using max print(max(lst, key=max)) # [3, 3, -1] # Maximum using min print(max(lst, key=min)) # [1, 1, 1]

Try it yourself in our interactive code shell:

**Related articles:**

- How to Find the Maximum of a List of Lists in Python?
- How to Find the Minimum of a List of Lists in Python?
- Python List Methods [Overview]
- Python List sort() – The Ultimate Guide
- Python Lists – Everything You Need to Know to Get Started
- Key with Maximum Value in Dict

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

**You build high-value coding skills by working on practical coding projects!**

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is ** YES!**, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.