search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Python | max method

Built-in Functions
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer Python
Tags

Python's max(~) method has two use cases:

  • When used with an iterable it returns the largest item in the iterable.

  • When used with multiple arguments, it returns the largest item out of the arguments.

Parameters

Used with an iterable

1. iterable | iterable

The iterable to retrieve the largest item for.

2. key | function | optional

Function to specify the ordering criteria. The function should take a single argument and return a key to use for ordering.

3. default | object | optional

The object to return if the provided iterable is empty.

Used with multiple arguments

1. arg1 | object

An object to use for comparison.

2. arg2 | object

An object to use for comparison.

3. args | object | optional

Unlimited number of objects to use for comparison.

4. key | function | optional

Function to specify the ordering criteria. The function should take a single argument and return a key to use for ordering.

Return value

The return value depends on the following cases:

Case

Return value

Used with an iterable

Largest item in the iterable

Used with multiple arguments

Largest item out of the arguments

Examples

Used with an iterable

To return the largest number in list x:

x = [4, 3, 9, 2, 11, 6]
max(x)
11

To return the largest item from list languages when ordered alphabetically:

languages = ['Spanish', 'French', 'English', 'Mandarin']
max(languages)
Spanish

Used with multiple arguments

To return the largest number from the provided arguments:

max(4, 3, 9, 2, 11, 6)
11

Key parameter

To return the largest item based on alphabetical order of the second letter of each element in iterable:

def check_second_letter(a):
return a[1]
languages = ['Spanish', 'French', 'English', 'Mandarin']
max(languages, key=check_second_letter)
'French'

French is returned as its second letter 'r' is the highest in terms of alphabetical order.

Default parameter

To return 'List is Empty' if the languages iterable is empty:

languages = []
max(languages, default='List is Empty')
List is Empty

Note that the default parameter is only applicable to the case when max method is used with an iterable input.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_down
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!