🌐
W3Schools
w3schools.com › python › ref_func_max.asp
Python max() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The max() function returns the item with the highest value, or the item with the highest value in an iterable.
🌐
Real Python
realpython.com › ref › builtin-functions › max
max() | Python’s Built-in Functions – Real Python
Python · >>> prices = { ... "banana": 1.20, ... "pineapple": 0.89, ... "apple": 1.57, ... "grape": 2.45, ... } >>> max(prices.items(), key=lambda item: item[1]) ('grape', 2.45) This example demonstrates how max() can help you identify the most expensive products by using the dictionary’s values for comparison.
Discussions

python max function using 'key' and lambda expression - Stack Overflow
What is use of the keyword key in max function? I know it is also used in context of sort function · Meaning of the lambda expression? How to read them? How do they work? These are all very noobish conceptual questions but will help me understand the language. It would help if you could give examples to explain. Thanks ... @InbarRose This page is actually now the top result on Google for python ... More on stackoverflow.com
🌐 stackoverflow.com
How do I make maximum and minimum work correctly in my function?
Hope this helps ! Ask anything if you don't understand ... def my_function() numbers = list() total = 0 while True: try: response = int( input() ) numbers.append(response) total += response #PYTHON RAISES A VALUE ERROR IF YOU PRESS ENTER INSTEAD OF GIVING AN INTEGER INPUT except ValueError: break max_number = max(numbers) min_number = min(numbers) average = total / len(numbers) print( "max number is " + str(max_number) ) print( "minimum number is " + str(min_number) ) print( "average is " + str(average) ) More on reddit.com
🌐 r/learnpython
8
1
October 19, 2019
[Python] Question! I am having trouble with min/max
I do't understand what key or lambda means, even after reading about them. And then the x: x[1] makes it extra confusing. Can anyone help me? First lambda: lambda is just a way to define a function inline. min([[1, 2], [3, 4], [9, 0]], key=lambda x: x[1]) is equivalent to: def f(x): return x[1] min([[1, 2], [3, 4], [9, 0]], key=f) The idea of the key argument (often used for various comparison functions like min, max or sort) is that you can pass in a function that will transform the data before comparing. So, if your key is lambda x: x[1] what you're really saying is: "take the second element of each list and compare those, then return the list that had the largest value." In the case above, comparing [1, 2], [3, 4], and [9, 0], we compare 2, 4, and 0. 4 is largest so we want to return [3, 4] More on reddit.com
🌐 r/learnpython
19
7
June 21, 2015
Problem with built-in function min and max

You probably overrode print or min in one of the prompts before. Try restarting the shell and it should work.

More on reddit.com
🌐 r/learnpython
2
1
July 26, 2020
People also ask

What is the use of the max() and min() functions in Python?
We use Python's max() and min() functions to get the maximum or minimum object out of a set of objects or max/min object out of objects in a given iterable.
🌐
interviewkickstart.com
interviewkickstart.com › home › blogs › learn › the max() function in python
The max() Function in Python | Interview Kickstart
What is the difference between the Python max() and min() functions?
Both compare items or items within an iterable on some basis and return a value. But the max() function in Python returns the max value after those comparisons, and the min() function returns the minimum value.
🌐
interviewkickstart.com
interviewkickstart.com › home › blogs › learn › the max() function in python
The max() Function in Python | Interview Kickstart
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-max-function
Python - max() function - GeeksforGeeks
July 15, 2025 - By default, it will return the string with the maximum lexicographic value. In this example, as max() is used to locate the largest item in Python, we are using max() to find maximum out of 3 string variable.
🌐
Programiz
programiz.com › python-programming › methods › built-in › max
Python max()
# find max among the arguments result = max(4, -5, 23, 5) print("The maximum number is:", result) ... If you need to find the smallest item, you can use the Python min() function.
🌐
Real Python
realpython.com › python-min-and-max
Python's min() and max(): Find Smallest and Largest Values – Real Python
January 18, 2025 - In the second example, the lowercase "w" comes after all the other strings’ initial letters. Note that there are two words that start with "w", "welcome" and "world". So, Python proceeds to look at the second letter of each word. The result is that max() returns "world" because "o" comes after "e".
🌐
Tutorialspoint
tutorialspoint.com › python › list_max.htm
Python List max() Method
Now let us find the maximum number in a list containing only numbers in the example below. list2 = [456, 700, 200] print("Max value element : ", str(max(list2)))
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
1 month ago - CPython implementation detail: len raises OverflowError on lengths larger than sys.maxsize, such as range(2 ** 100).
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › the max() function in python
The max() Function in Python | Interview Kickstart
December 23, 2024 - If multiple iterables are passed as arguments for max, max() compares the first items of all iterables and returns the iterable whose first value is the maximum. If all first values are equal, it compares the second values of all, and so on. Here, we take a look at how you can use the Python function max() in the context of the data structure list, dictionary, string, or integer next time you need it:
🌐
Hyperskill
hyperskill.org › university › python › max-in-python
Python max()
December 25, 2025 - This course is dedicated to core Python skills that will give you a solid base and allow you to pursue any further direction, be it Backend Development or Data Science. ... The max() function in Python is commonly employed to identify the value from a provided collection of elements.
🌐
ThePythonGuru
thepythonguru.com › python-builtin-functions › max
Python max() function - ThePythonGuru.com
January 7, 2020 - The following is another example where we compare strings based on its length instead of their ASCII values. Try it out: print(max(("python", "lua", "ruby"))) print(max(("python", "lua", "ruby"), key=len)) Output · Input · There also exists a complementary function called min() which finds ...
🌐
Tutorialspoint
tutorialspoint.com › python › number_max.htm
Python max() Function
# Creating a list List = [74,587,24,92,4,2,7,46] res = max(List) print("The largest number in the list is: ", res) While executing the above code we get the following output − ... In the example below, a list of equal length of strings is created.
🌐
iO Flood
ioflood.com › blog › python-max-function-guide-uses-and-examples
Python Max() Function Guide: Uses and Examples
February 6, 2024 - In this example, max() finds the largest value in the list. The max() function accepts two types of arguments: Two or more objects (like integers, floats, strings, etc.) ... The max() function returns the largest item in the iterable or the ...
🌐
Python Examples
pythonexamples.org › python-max-function
max() Builtin Function
a = [18, 52, 23, 41, 32] largest = max(a) print(f'Largest number in the list is : {largest}.') ... In this example, we will take five numbers and find the largest number of these using max() function.
🌐
Unstop
unstop.com › home › blog › python max() function | all use cases with code examples
Python max() Function | All Use Cases With Code Examples
February 12, 2025 - When working with strings, the max() function can be used to find the "largest" string based on lexicographical (or alphabetical) order. This means the strings are compared character by character, just like how words are arranged in a dictionary. In this basic Python program example, we have illustrated how to use the max() function to compare strings lexicographically (alphabetical order) and find the maximum string.
🌐
Vultr Docs
docs.vultr.com › python › built-in › max
Python max() - Find Maximum Value | Vultr Docs
September 27, 2024 - python Copy · data = {'a': 100, 'b': 200, 'c': 50} max_value = max(data.values()) print(max_value) Explain Code · This example directs max() to evaluate the values of the dictionary data, resulting in 200 being identified as the maximum value.
🌐
Devcuriosity
devcuriosity.com › manual › details › python-max-function
Python - max() function with examples - DevCuriosity
In this example, with function ord() we can see that letter 'w' has the biggest value - 119. my_list = ["www", "devcuriosity", "com"] my_max = max(my_list, key=len) print(my_max) # devcuriosity
🌐
Python Tutorial
pythontutorial.net › home › python built-in functions › python max() function
Python max(): Return the Maximum Item
September 15, 2022 - The following example uses the max() function to get the largest number: result = max(1, 2, 3) print(result) # 3Code language: Python (python)
Top answer
1 of 8
417

lambda is an anonymous function, it is equivalent to:

def func(p):
   return p.totalScore     

Now max becomes:

max(players, key=func)

But as def statements are compound statements they can't be used where an expression is required, that's why sometimes lambda's are used.

Note that lambda is equivalent to what you'd put in a return statement of a def. Thus, you can't use statements inside a lambda, only expressions are allowed.


What does max do?

max(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its largest item. With two or more arguments, return the largest argument.

So, it simply returns the object that is the largest.


How does key work?

By default in Python 2 key compares items based on a set of rules based on the type of the objects (for example a string is always greater than an integer).

To modify the object before comparison, or to compare based on a particular attribute/index, you've to use the key argument.

Example 1:

A simple example, suppose you have a list of numbers in string form, but you want to compare those items by their integer value.

>>> lis = ['1', '100', '111', '2']

Here max compares the items using their original values (strings are compared lexicographically so you'd get '2' as output) :

>>> max(lis)
'2'

To compare the items by their integer value use key with a simple lambda:

>>> max(lis, key=lambda x:int(x))  # compare `int` version of each item
'111'

Example 2: Applying max to a list of tuples.

>>> lis = [(1,'a'), (3,'c'), (4,'e'), (-1,'z')]

By default max will compare the items by the first index. If the first index is the same then it'll compare the second index. As in my example, all items have a unique first index, so you'd get this as the answer:

>>> max(lis)
(4, 'e')

But, what if you wanted to compare each item by the value at index 1? Simple: use lambda:

>>> max(lis, key = lambda x: x[1])
(-1, 'z')

Comparing items in an iterable that contains objects of different type:

List with mixed items:

lis = ['1','100','111','2', 2, 2.57]

In Python 2 it is possible to compare items of two different types:

>>> max(lis)  # works in Python 2
'2'
>>> max(lis, key=lambda x: int(x))  # compare integer version of each item
'111'

But in Python 3 you can't do that any more:

>>> lis = ['1', '100', '111', '2', 2, 2.57]
>>> max(lis)
Traceback (most recent call last):
  File "<ipython-input-2-0ce0a02693e4>", line 1, in <module>
    max(lis)
TypeError: unorderable types: int() > str()

But this works, as we are comparing integer version of each object:

>>> max(lis, key=lambda x: int(x))  # or simply `max(lis, key=int)`
'111'
2 of 8
25

Strongly simplified version of max:

def max(items, key=lambda x: x):
    current = item[0]
    for item in items:
        if key(item) > key(current):
            current = item
    return current

Regarding lambda:

>>> ident = lambda x: x
>>> ident(3)
3
>>> ident(5)
5

>>> times_two = lambda x: 2*x
>>> times_two(2)
4
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python list max() function
Python List max() Function - Spark By {Examples}
May 31, 2024 - You can use the max() function in Python to get the maximum value from a list or an iterable. For example, the max() takes the list named mylist as an argument and returns the largest number in the list, which is 66.