ScanSkill

max

This max() function returns the largest item in an iterable or the largest of two or more arguments.

Syntax

max(collection[, key])

Here,

  • collection: Required. A comma-separated list of objects or an iterable sequence.
  • key: Optional. Specifies a one-argument ordering function; must be in keyword form.

Note: When comparing sequences lexical comparison is used.

Examples

  • Comparing numbers to get the largest one
>>> max(1, 2, 3)
3
>>> max('A', 'a', 'b')
'b'
>>> max([1, 2], [2, 1], [3, 1])
[3, 1]
>>> max(str([1, 2]), str([2, 1]), str([3, 1]))
'[3, 1]'
  • Comparing strings
>>> max('apple', 'Pear', key=lambda x: x.upper())
'Pear'
>>> max('apple', 'Pear')
'apple'