• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] Python List max()

Python List max()

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)`

### 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.

1. The first element of each inner list.
2. The i-th element of each inner list.
3. The sum of inner list elements.
4. The maximum of inner list elements.
5. 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?

1. The first element of each inner list. The maximum is `[3, 3, -1]`.
2. The i-th element of each inner list (`i = 2`). The maximum is `[1, 1, 1]`.
3. The sum of inner list elements. The maximum is `[3, 3, -1]`.
4. The maximum of inner list elements. The maximum is `[3, 3, -1]`.
5. 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:

## Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

https://www.sickgaming.net/blog/2020/07/...-list-max/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] Python One Line Replace xSicKxBot 0 5 08-13-2020, 07:49 PM Last Post: xSicKxBot [Tut] Python One Line Regex Match xSicKxBot 0 10 08-12-2020, 06:43 PM Last Post: xSicKxBot [Tut] Lost Your Sight? How to Code as a Blind or Visually-Impaired Python Developer xSicKxBot 0 10 08-11-2020, 02:49 PM Last Post: xSicKxBot [Tut] Python Infinity xSicKxBot 0 8 08-10-2020, 02:16 PM Last Post: xSicKxBot [Tut] Python One Line With Statement xSicKxBot 0 17 08-06-2020, 03:59 PM Last Post: xSicKxBot [Tut] PEP 8: Hanging Indentation and Closing Brackets in Python xSicKxBot 0 17 08-04-2020, 09:51 AM Last Post: xSicKxBot [Tut] How to Create a Database Table with pyodbc in Python xSicKxBot 0 19 08-03-2020, 08:37 AM Last Post: xSicKxBot [Tut] Python One Line Return if xSicKxBot 0 15 08-01-2020, 07:08 PM Last Post: xSicKxBot [Tut] Top 5 Python Freelancer Jobs to Earn \$51 per Hour on Upwork or Fiverr xSicKxBot 0 20 07-31-2020, 06:46 PM Last Post: xSicKxBot [Tut] Python One Line Reverse Shell xSicKxBot 0 19 07-30-2020, 04:50 PM Last Post: xSicKxBot

Forum Jump:

 Upcoming Events

 Latest Threads AppleInsider - Trump orders ByteDance to... Last Post: xSicKxBot Today 03:17 PM » Replies: 0 » Views: 1 Microsoft - Microsoft’s accessibility gu... Last Post: xSicKxBot Today 03:17 PM » Replies: 0 » Views: 1 News - Video: More Madness Ensues In The... Last Post: xSicKxBot Today 03:17 PM » Replies: 0 » Views: 1 News - Commander Keen In Keen Dreams Com... Last Post: xSicKxBot Today 03:17 PM » Replies: 0 » Views: 1 News - HBO Max Adds Adds Disclaimer For ... Last Post: xSicKxBot Today 03:17 PM » Replies: 0 » Views: 1 (Free Game Key) Remnant: From the Ashes ... Last Post: xSicKxBot Today 09:14 AM » Replies: 0 » Views: 5 News - Bedrock Beta: 1.16.100.51 Last Post: xSicKxBot Today 07:16 AM » Replies: 0 » Views: 3 News - Get a job: Join Robot Entertainme... Last Post: xSicKxBot Today 07:16 AM » Replies: 0 » Views: 3 News - Don’t Miss: Why multiplayer almos... Last Post: xSicKxBot Today 07:16 AM » Replies: 0 » Views: 3 Mobile - Apple and Google kick Fortnite ... Last Post: xSicKxBot Today 06:07 AM » Replies: 0 » Views: 3