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

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Find Min and Max values in a list of tuples in Python
  2. Get the Max value in a Tuple in Python
  3. Get the Min value in a Tuple in Python

# 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
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)

find min and max values in list of tuples

The code for this article is available on GitHub

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
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
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
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')

getting min and max values according to different criteria

The code for this article is available on GitHub

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
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)

find min and max values in list of tuples using itemgetter

The code for this article is available on GitHub

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
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'
The code for this article is available on GitHub

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
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
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
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
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
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'
The code for this article is available on GitHub

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
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
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
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
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev