# Find Min and Max values in Tuple or List of Tuples in Python

Last updated: Apr 9, 2024
5 min

## #Find Min and Max values in a list of tuples in Python

To find the min and max values in a list of tuples:

1. Use the `min()` and `max()` functions.
2. Pass the `key` argument to the functions.
3. Select the element in the tuple to be compared.
main.py
```Copied!```my_list = [(100, 1),  (100, 2), (100, 3)]

# โ get the Min tuple in list of tuples
min_tuple = min(my_list, key=lambda tup: tup[1])

print(min_tuple)  # ๐๏ธ (100, 1)

# ------------------------------------------------------

# โ get the Max tuple in a list of tuples

max_tuple = max(my_list, key=lambda tup: tup[1])

print(max_tuple)  # ๐๏ธ (100, 3)
``````

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

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

The functions take an optional `default` keyword argument which is used to specify a value to return if the provided iterable is empty.

main.py
```Copied!```result = max((), default=0)

print(result)  # ๐๏ธ 0
``````

If the iterable is empty and the `default` keyword argument is not provided, the functions raise a `ValueError`.

The `key` argument specifies a one-argument ordering function like the one used for `list.sort()`.
main.py
```Copied!```my_list = [(100, 1),  (100, 2), (100, 3)]

min_tuple = min(my_list, key=lambda tup: tup[1])

print(min_tuple)  # ๐๏ธ (100, 1)
``````

We selected the tuple item at index `1` to compare the second item in the tuples.

## #Getting the min and max values according to different criteria

You can also use the `key` argument to get the min and max values in a list of tuples according to different criteria.

main.py
```Copied!```my_list = [(100, 'a'),  (100, 'ab'), (100, 'abc')]

# โ get value with min length in the list of tuples
min_tuple = min(my_list, key=lambda tup: len(tup[1]))

print(min_tuple)  # ๐๏ธ (100, 'a')

# ------------------------------------------------------

# โ get value with max length in the list of tuples

max_tuple = max(my_list, key=lambda tup: len(tup[1]))

print(max_tuple)  # ๐๏ธ (100, 'abc')
``````

We passed the second item in each tuple to the `len()` function to get the string's length.

The function you provide for the `key` argument can be used to get the `min` and `max` values according to different criteria.

## #Find Min and Max values in a list of tuples using itemgetter

You can also use the `operator.itemgetter` class to find the min and max values in a list of tuples.

main.py
```Copied!```from operator import itemgetter

my_list = [(100, 1),  (100, 2), (100, 3)]

# โ get the Min tuple in list of tuples
min_tuple = min(my_list, key=itemgetter(1))

print(min_tuple)  # ๐๏ธ (100, 1)

# ------------------------------------------------------

# โ get the Max tuple in a list of tuples

max_tuple = max(my_list, key=itemgetter(1))

print(max_tuple)  # ๐๏ธ (100, 3)
``````

The code sample uses `operator.itemgetter` instead of using a lambda function.

The operator.itemgetter() class returns a callable object that fetches the items at the specified indices.

For example, `x = itemgetter(1)` and then calling `x(my_tuple)`, returns `my_tuple[1]`.

## #Get the Max value in a Tuple in Python

Use the `max()` function to get the max value in a tuple.

The `max()` function returns the smallest item in an iterable (such as a tuple).

main.py
```Copied!```my_tuple = (1, 3, 5, 7, 9)

# โ Get the max value in a tuple
max_value = max(my_tuple)

print(max_value)  # ๐๏ธ 9

# -----------------------------------------

# โ Get the item with max length in a tuple

my_tuple = ('a', 'ab', 'abc')

max_length = max(my_tuple, key=lambda val: len(val))

print(max_length)  # ๐๏ธ 'abc'
``````

We used the `max()` function to get the max value in a tuple.

The max function returns the largest item in an iterable or the largest of two or more arguments.

main.py
```Copied!```my_tuple = (15, 45, 30)

result = max(my_tuple)

print(result)  # ๐๏ธ 45
``````

The function can also be called with two or more positional arguments.

main.py
```Copied!```result = max(15, 45, 30)

print(result)  # ๐๏ธ 45
``````

The function takes an optional `default` keyword argument which is used to specify a value to return if the provided iterable is empty.

main.py
```Copied!```result = max((), default=0)

print(result)  # ๐๏ธ 0
``````

If the iterable is empty and the `default` keyword argument is not provided, the function raises a `ValueError`.

If you need to get the tuple element with max length, use the `key` keyword argument.

main.py
```Copied!```my_tuple = ('a', 'ab', 'abc')

max_length = max(my_tuple, key=lambda val: len(val))

print(max_length)  # ๐๏ธ 'abc'
``````

The `key` argument specifies a one-argument ordering function like the one used for `list.sort()`.

We set the argument to a `lambda` function that passes each tuple element to the `len()` function and returns the result.

THe `max()` function returns the tuple element with `max` length.

## #Get the Min value in a Tuple in Python

Use the `min()` function to get the min value in a tuple.

The `min()` function returns the smallest item in an iterable (such as a tuple).

main.py
```Copied!```my_tuple = (1, 3, 5, 7, 9)

# โ Get min value in a tuple
min_value = min(my_tuple)

print(min_value)  # ๐๏ธ 1

# -----------------------------------------

# โ Get the item with min length in a tuple

my_tuple = ('a', 'ab', 'abc')

min_length = min(my_tuple, key=lambda val: len(val))

print(min_length)  # ๐๏ธ 'a'
``````

We used the `min()` function to get the min value in a tuple.

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
```Copied!```my_tuple = (10, 5, 20)

result = min(my_tuple)

print(result)  # ๐๏ธ 5
``````

The function can also be called with or more positional arguments.

main.py
```Copied!```result = min(10, 5, 20)

print(result)  # ๐๏ธ 5
``````

The function takes an optional `default` keyword argument which is used to specify a value to return if the provided iterable is empty.

main.py
```Copied!```result = min((), default=0)

print(result)  # ๐๏ธ 0
``````

If the iterable is empty and the `default` keyword argument is not provided, the function raises a `ValueError`.

You can use the `key` argument of the `min()` function to get the min value in a tuple according to different criteria.

Here is an example that finds the value with the minimum length in a tuple.

main.py
```Copied!```my_tuple = ('a', 'ab', 'abc')

min_length = min(my_tuple, key=lambda val: len(val))

print(min_length)  # ๐๏ธ 'a'
``````

The lambda function gets called with each element of the tuple and returns its length.

The `min()` function is then called with the length of the elements and returns the `min` length in the tuple.