Min and Max value in list of tuples-Python
Last Updated :
19 Feb, 2025
The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maximum of 8, while the second elements (3, 7, 11, 6) have a minimum of 3 and a maximum of 11. The result is (2, 8) for the first column and (3, 11) for the second.
Using zip()
zip(*a) transposes the list of tuples, effectively grouping each element from corresponding positions (columns) into separate sequences. We can then apply min() and max() directly to each sequence.
Python
a = [(2, 3), (4, 7), (8, 11), (3, 6)]
x, y = zip(*a)
res1 = (min(x), max(x)) # first
res2 = (min(y), max(y)) # second
print(res1)
print(res2)
Explanation: zip(*a) transposes the list of tuples, grouping first and second elements separately. min() and max() are applied to each group, forming res1 and res2 with the minimum and maximum values.
Using generator function
This method manually extracts elements from each tuple using generator function. These elements are then passed into min() and max() functions.
Python
a = [(2, 3), (4, 7), (8, 11), (3, 6)]
res1 = (min(x[0] for x in a), max(x[0] for x in a))
res2 = (min(x[1] for x in a), max(x[1] for x in a))
print(res1)
print(res2)
Explanation:(x[0] for x in a) extracts the first element from each tuple and (x[1] for x in a) extracts the second. min() and max() are applied to both sequences, forming tuples res1 and res2 with the minimum and maximum values.
Using map()
This method manually extracts elements from each tuple using map() and lambda functions. These elements are then passed into min() and max() functions.
Python
a = [(2, 3), (4, 7), (8, 11), (3, 6)]
res1 = (min(map(lambda x: x[0], a)), max(map(lambda x: x[0], a)))
res2 = (min(map(lambda x: x[1], a)), max(map(lambda x: x[1], a)))
print(res1)
print(res2)
Explanation: map(lambda x: x[0], a) and map(lambda x: x[1], a) extract the first and second elements from each tuple. min() and max() are applied to get res1 and res2 with the minimum and maximum values.
Using pandas
Pandas is an external library designed for data analysis. It treats tuples as rows and creates columns for their elements. We can leverage .min() and .max() on each column.
Python
import pandas as pd
a = [(2, 3), (4, 7), (8, 11), (3, 6)]
df = pd.DataFrame(a, columns=['first', 'second']) # convert`a` to panda dataframe
res1 = (df['first'].min(), df['first'].max())
res2 = (df['second'].min(), df['second'].max())
print(res1)
print(res2)
Output(np.int64(2), np.int64(8))
(np.int64(3), np.int64(11))
Explanation: df['first'].min() and df['first'].max() get the minimum and maximum values from the 'first' column, forming res1. Similarly, df['second'].min() and df['second'].max() form res2 from the 'second' column.
Similar Reads
Python | Max/Min of tuple dictionary values Sometimes, while working with data, we can have a problem in which we need to find the min/max of tuple elements that are received as values of dictionary. We may have a problem to get index wise min/max. Let's discuss certain ways in which this particular problem can be solved. Method #1 : Using tu
5 min read
Python - Minimum in tuple list value Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the minimum of list as tuple attribute. Letâs discuss certai
5 min read
Python | Get first element with maximum value in list of tuples In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Maximum and Minimum value from two lists - Python Finding the maximum and minimum values from two lists involves comparing all elements to determine the highest and lowest values. For example, given two lists [3, 5, 7, 2, 8] and [4, 9, 1, 6, 0], we first examine all numbers to identify the largest and smallest. In this case, 9 is the highest value
3 min read
Python - Records Maxima in List of Tuples Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using ma
5 min read