Find Minimum and Maximum values in a list of tuples in Python
Let us assume we have an e-commerce website displaying the cost price and selling price of the products. These prices can be represented in the form of tuples inside the products list.
Now suppose the admin wants to analyze the profit generated by selling the products. Since profit can be analyzed with selling and cost prices, we will find the max and minimum cost and selling price of the products. Thus in this tutorial, we will learn how to find minimum and maximum values in a list of tuples in Python.
Method 1: Applying Max() and Min() functions
The products list contains tuples where index 0 represents cost price and index 1 represents selling price.
The min function takes two arguments. The first is the products list and the key represents the basis on which the minimum or maximum price is found.
The key in our case is a lambda function. The lambda function takes res as an argument and returns the expression that will be executed. The expression in this case gives the first index of every tuple.
The output of the min function is the tuple having a minimum value of cost price that is (70,100). From this tuple, we choose the cost price(index 0) as our final output.
Similarly, we repeat this step to find the minimum and maximum costs and selling prices.
products = [(100, 30), (120, 110), (70, 100), (95, 60)] print ("The original products list is : " + str(products)) min_cp = min(products, key=lambda res: res[0])[0] max_cp = max(products, key=lambda res: res[0])[0] min_sp = min(products, key=lambda res: res[1])[1] max_sp = max(products, key=lambda res: res[1])[1] min_max_cp=min_cp,max_cp min_max_sp=min_sp,max_sp print ("The min and max of cost price : " + str(min_max_cp)) print ("The min and max of selling price : " + str(min_max_sp))
Output:
The original products list is : [(100, 30), (120, 110), (70, 100), (95, 60)] The min and max of cost price : (70, 120) The min and max of selling price : (30, 110)
Method 2: Applying map() and zip()
In this method, we initially apply the map function which takes two arguments. The first argument is the max/min function which we will apply over every element of the iterable. The iterable is the second argument which in our case is zip(*products). The zip function pairs or maps the values of each tuple together.
We pass *products and not only products. This is because *products unpack the list making every tuple a separate argument. Since the map function returns a map object, we convert it into a list using the list() function. Thus we finally get the maximum cost and selling price and minimum cost and selling price respectively.
products = [(100, 30), (120, 110), (70, 100), (95, 60)] print ("The original products list is : " + str(products)) min_cp_sp = list(map(max, zip(*products))) max_cp_sp = list(map(min, zip(*products))) print ("The maximum cost price and selling price : " + str(min_cp_sp)) print ("The minimum cost price and selling price : " + str(max_cp_sp))
Output:
The original products list is : [(100, 30), (120, 110), (70, 100), (95, 60)] The maximum cost price and selling price : [120, 110] The minimum cost price and selling price : [70, 30]
Thus we have reached the end of this tutorial on how to find minimum and maximum values in the list of tuples. To read more about the list and tuple in Python click on the following link: Differences between list and tuple in Python
Leave a Reply