Python | Min and Max value in list of tuples
Last Updated :
17 Jun, 2023
The computation of min and max values is a quite common utility in any programming domain be it development or any other field that includes any programming constructs. Sometimes, data can come in the format of tuples and min and max operations have to be performed in them. Let’s discuss certain ways in which this is handled.
Min and Max value in the list of tuples in Python Examples
To find the minimum and maximum value in the list of tuples in Python, there can be so many ways. Some of the ways/methods are listed below.
- Finding min and max in the list with min() and max() in Python
- Finding min and max in list with map() + zip() in Python
- Using loop+min()+max() methods to find min and max in the list in Python
- Python min and max finding in the list with sorted() function
- Finding min and max in the list with reduce()+ lambda in Python
- Python min and max finding in the list with NumPy, arg min and arg max functions
Finding min and max in the list with min() and max() in Python
In this method, we use the Python inbuilt min and max functions to perform the task of getting the minimum and maximum value of a particular element position.
Python3
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
print ( "The original list is : " + str (test_list))
res1 = min (test_list)[ 0 ], max (test_list)[ 0 ]
res2 = min (test_list)[ 1 ], max (test_list)[ 1 ]
print ( "The min and max of index 1 : " + str (res1))
print ( "The min and max of index 2 : " + str (res2))
|
Output :
The original list is : [(2, 3), (4, 7), (8, 11), (3, 6)]
The min and max of index 1 : (2, 8)
The min and max of index 2 : (3, 11)
Finding min and max in list with map() + zip() in Python
This is the more elegant way to perform this particular task. In this task, we use map() function to link the elements to the zip functions that accumulate to perform the functionality of the min function or max function.
Python3
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
print ( "The original list is : " + str (test_list))
res1 = list ( map ( max , zip ( * test_list)))
res2 = list ( map ( min , zip ( * test_list)))
print ( "The indices wise maximum number : " + str (res1))
print ( "The indices wise minimum number : " + str (res2))
|
Output :
The original list is : [(2, 3), (4, 7), (8, 11), (3, 6)]
The indices wise maximum number : [8, 11]
The indices wise minimum number : [2, 3]
Using for loop+min()+max() methods to find min and max in list in Python
In this example, we have a list of tuples. We are using a for loop to traverse the list of tuples and store the first and second locations in two lists and store the minimum and maximum in two tuples.
Python3
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
print ( "The original list is : " + str (test_list))
x = []
y = []
for i in test_list:
x.append(i[ 0 ])
y.append(i[ 1 ])
res1 = ( min (x), max (x))
res2 = ( min (y), max (y))
print ( "The min and max of index 1 : " + str (res1))
print ( "The min and max of index 2 : " + str (res2))
|
Output :
The original list is : [(2, 3), (4, 7), (8, 11), (3, 6)]
The min and max of index 1 : (2, 8)
The min and max of index 2 : (3, 11)
Python min and max finding in list with sorted() function
First, create a list of values that contains all the values in the tuples of the test_list. Then we sort the values list using the sorted() function. Finally, we extract the minimum and maximum values from the sorted list using indexing.
Python3
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
values = [val for tup in test_list for val in tup]
sorted_values = sorted (values)
min_val = sorted_values[ 0 ]
max_val = sorted_values[ - 1 ]
print ( "The minimum value is: " , min_val)
print ( "The maximum value is: " , max_val)
|
Output :
The minimum value is: 2
The maximum value is: 11
Finding min and max in a list with reduce()+ lambda in Python
The approach using the reduce() function from the functools module to find the minimum and maximum value in the first and second elements of each tuple in a list involves iterating over the elements of the list and applying the min() or max() function to each element.
Python3
from functools import reduce
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
print ( "The original list is:" , test_list)
min_first_element = reduce ( lambda x, y: min (x, y[ 0 ]),
test_list, float ( "inf" ))
max_first_element = reduce ( lambda x, y: max (x, y[ 0 ]),
test_list, float ( "-inf" ))
min_second_element = reduce ( lambda x, y: min (x, y[ 1 ]),
test_list, float ( "inf" ))
max_second_element = reduce ( lambda x, y: max (x, y[ 1 ]),
test_list, float ( "-inf" ))
print ( "The min and max of index 0:" , (min_first_element,
max_first_element))
print ( "The min and max of index 1:" , (min_second_element,
max_second_element))
|
Output :
The original list is: [(2, 3), (4, 7), (8, 11), (3, 6)]
The min and max of index 0: (2, 8)
The min and max of index 1: (3, 11)
Python min and max finding in list with numpy,argmin and argmax functions
Here we will be using Numpy to convert the list of tuples into a Numpy array. Now we use argmin and argmax functions of Numpy to get the index of the minimum and maximum values in each column of the array. Finally, we use these indices to extract the corresponding tuples from the original list.
Python3
import numpy as np
test_list = [( 2 , 3 ), ( 4 , 7 ), ( 8 , 11 ), ( 3 , 6 )]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
min_index_0, max_index_0 = np.argmin(arr[:, 0 ])
, np.argmax(arr[:, 0 ])
min_index_1, max_index_1 = np.argmin(arr[:, 1 ])
, np.argmax(arr[:, 1 ])
res1 = arr[min_index_0][ 0 ], arr[max_index_0][ 0 ]
res2 = arr[min_index_1][ 1 ], arr[max_index_1][ 1 ]
print ( "The min and max of index 1 : " + str (res1))
print ( "The min and max of index 2 : " + str (res2))
|
Output:
The original list is : [(2, 3), (4, 7), (8, 11), (3, 6)]
The min and max of index 1 : (2, 8)
The min and max of index 2 : (3, 11)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...