Page Contents

Max function in Python returns the largest item from an iterable or from two and more arguments .

## Syntax of max function in Python

max(collection[,key])

## Parameters of max function in Python

- collection = collection is the list of objects separated by comma or any iterable .
- key =
**Optional**. It is a function that will be applied to every object of the collection or to every item in the iterable . Must be in keyword form .

## Return type of max function in Python

It returns the the largest item from an iterable or from two and more arguments .

## Examples of max function in Python

#### Example 1: Get the maximum number from a list of numbers

```
# Get the maximum number from a list of numbers
max_num=max(10,5,16,34,6)
print(max_num)
```

34

In the above code we have find the maximum number from the given numbers using `max()`

function . Here as collection parameter we have used a list of objects separated by comma .

#### Example 2: Get the largest item from an iterable

```
# get the largest item from a list
max_item = max([10,5,16,34,6])
print("max item from the list is =",max_item)
# get the largest item from a tuple
max_item = max((10,5,16,34,6))
print("max item from the tuple is =",max_item)
# get the largest item from a set
max_item = max({10,5,16,34,6})
print("max item from the set is =",max_item)
# get the largest item from a dictionary
max_item = max({10:1,5:2,16:3,34:4,6:5})
print("max item from the dictionary is =",max_item)
# get the largest item from a string
max_item = max("32576")
print("max item from the string is =",max_item)
```

max item from the list is = 34 max item from the tuple is = 34 max item from the set is = 34 max item from the dictionary is = 34 max item from the string is = 7

In the above code we have got largest item from various iterable using `max()`

function .

#### Example 3: Max function with key parameter

```
# getting the largest item from a list
# without using key parameter
max_item=max(["1","2","11","22","3"])
print("max item without key parameter =",max_item)
# getting the largest item from a list
# using key parameter
max_item=max(["1","2","11","22","3"],key=int)
print("max item with key parameter =",max_item)
```

max item without key parameter = 3 max item with key parameter = 22

- In the above code first we have got the largest element from the list using
`max()`

function without`key`

parameter . As`"3"`

is the largest item so it returned`"3"`

. - Then we have used
`max()`

function with key parameter to get the largest element from the list . We have used**int()**function as`key`

parameter . We do not need to call the function we only need to mention the function name . As a result all the string of the list is converted to integer . Now`22`

is the largest integer in the list . So the program returned`22`

.

#### Example 4: Get the largest pair from a list whose multiplication is the highest

```
# getting the largest pair from a list
# whose multiplication is the highest
# defining the key function
def multiply(mytuple):
multiplication=1
for no in mytuple:
multiplication*=no
return multiplication
max_pair=max([(5,4),(6,3),(7,2)],key=multiply)
print("max pair is =",max_pair)
```

max pair is = (5, 4)

- In the above code we have defined a function
`multiply`

which will take a tuple of numbers and muliply the numbers and return their multiplication . - Then we have used this function as the key parameter of
`max()`

function . Now`multiply()`

function has converted`(5,4)`

to`20`

,`(6,3)`

to`18`

and`(7,2)`

to`14`

. As`20`

is the largest number so program returned`(5,4)`

.

#### Example 5: Use lambda expression as key parameter of max() function

```
# getting the largest pair from a list
# whose multiplication is the highest
max_pair=max([(5,4),(6,3),(7,2)],key=lambda x:x[0]*x[1])
print("max pair is =",max_pair)
```

max pair is = (5, 4)

In the above code we have solved the previous problem using lambda expression as key parameter .

**Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment **.