Open In App

Python | Min and Max value in list of tuples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




# Python3 code to demonstrate
# min and max in list of tuples
# using min() and max()
 
# initializing list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using min() and max()
# to get min and max in list of tuples
res1 = min(test_list)[0], max(test_list)[0]
res2 = min(test_list)[1], max(test_list)[1]
 
# printing result
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




# Python3 code to demonstrate
# min and max in list of tuples
# using map() + zip()
 
# initializing list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using map() + zip()
# to get min and max in list of tuples
res1 = list(map(max, zip(*test_list)))
res2 = list(map(min, zip(*test_list)))
 
# printing result
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




# Python3 code to demonstrate
# min and max in list of tuples
# using min() and max()
 
# initializing list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using min() and max()
# to get min and max in list of tuples
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))
# printing result
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




# initializing list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# create a list of all values in the tuples
values = [val for tup in test_list for val in tup]
 
# sort the list of values
sorted_values = sorted(values)
 
# find the min and max values
min_val = sorted_values[0]
max_val = sorted_values[-1]
 
# printing result
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
 
# initialize list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# print original list
print("The original list is:", test_list)
 
# use reduce() and min() to find the minimum value in
# the first element of each tuple
min_first_element = reduce(lambda x, y: min(x, y[0]),
                           test_list, float("inf"))
 
# use reduce() and max() to find the maximum value in
# the first element of each tuple
max_first_element = reduce(lambda x, y: max(x, y[0]),
                           test_list, float("-inf"))
 
# use reduce() and min() to find the minimum value in
# the second element of each tuple
min_second_element = reduce(lambda x, y: min(x, y[1]),
                            test_list, float("inf"))
 
# use reduce() and max() to find the maximum value in
# the second element of each tuple
max_second_element = reduce(lambda x, y: max(x, y[1]),
                            test_list, float("-inf"))
 
# print results
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))
#This code is contributed by Edula Vinay Kumar Reddy


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
 
# Initializing list
test_list = [(2, 3), (4, 7), (8, 11), (3, 6)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Converting to numpy array
arr = np.array(test_list)
 
# Using argmin and argmax along axis=0
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])
 
# Getting values at these indices
res1 = arr[min_index_0][0], arr[max_index_0][0]
res2 = arr[min_index_1][1], arr[max_index_1][1]
 
# Printing result
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)


Last Updated : 17 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads