Finding the Min and Max Value in a list of Tuples using Python


Introduction

The Python language is composed of several data structures and from the list is the most common one. The elements inside the list can be from any data type like integer, string, or float data type. And the elements are represented inside the square brackets and separated by comma. Working with the list data structure is the most interesting fundamental. Tuples are one of the data structures which can hold elements of different data types. The values that can hold tuples are integers, strings, or other tuples.

To find the Min and Max Value in a list of Tuples

Tuples can have duplicate sequences and once these elements are assigned, they cannot be changed. It is placed inside these “()” brackets and separated by a comma. It does not support deleting the elements in the tuple.

Approaches

Approach 1: Using the numpy library

Approach 2: Using the min and max functions

Approach 3: Using the lambda function

Approach 1: Python Program to find the Maximum and Minimum in a list of Tuples using the Numpy Module

The elements are given in the form of tuples in the list data structure as input and the output is in the form of tuples using the predefined library called numpy modules.

Algorithm

Step 1 :The input is given with tuple elements and the lambda functions come into action using the key parameters like “a” and the numpy module is imported.

Step 2 :The list data structure is created to hold the integer values.

Step 3 :The function is used to get the minimum of the elements in the list using the min() method.

Step 4 :The result is stored in the variable named “minimum_value”.

Step 5 :Then the print function will return the list after performing the operations.

Example

#importing the library
import numpy as np
#Creating the list data structure
list1 = [(1,1), (3, 3), (2,2), (4,4)]
#input list is converted to numpy array
num = np.array(list1)
#To get the required value min function is declared
minimum_list = np.min(num, axis=0)
#To get the required value max function is declared
maximum_list = np.max(num, axis=0)
#print function to get the output
print("Minimum value in the input:", minimum_list)
print("Maximum value in the input:", maximum_list)

Output

Minimum value in the input: [1 1]
Maximum value in the input: [4 4]

Approach 2: Python Program to find the Minimum and Maximum Values in a list of Tuples using the Functions

Algorithm

Step 1 :The input is given.

Step 2 :In this case, separate functions are defined to carry out the operations.

Step 3 :Every element is considered while calculating both maximum and minimum values using `max()’ and `min()`, respectively.

Step 4 :The ‘max()’ function is used to get the highest value of the given two−list data structure.

Step 5 :The output is printed finally with the values.

Example

#Creating the list data structure
list1 = [(1,1), (3, 3), (2,2), (4,4)]
#To get the required value min function is declared
minimum_list = min(list1)
#To get the required value max function is declared
maximum_list = max(list1)
#print function to get the output
print("Minimum value in the input:", minimum_list)
print("Maximum value in the input:", maximum_list)

Output

Minimum value in the input: (1, 1)
Maximum value in the input: (4, 4)

Approach 3: Python program to find the Maximum and Minimum value in alist of Tuples using the Lambda Function

At times we might want to specify additional conditions when searching for max or min values. Python offers a helpful feature where we can apply predicates using lambda functions within these built−in methods.

Algorithm

Step 1 :At times we might want to specify additional conditions when searching for max or min values. Python offers a helpful feature where we can apply predicates using lambda functions within these built−in methods.

Step 2 :The resulting filtered collection is then passed into `min()` to obtain the desired smallest value efficiently.

Step 3 :The ‘max()’ function is used to get the highest value of the given two−list data structure.

Step 4 :The output is returned in the form of tuples that is inside the brackets.

Example

#Creating the list data structure
list1 = [(1,1), (3, 3), (2,2), (4,4)]
num = lambda a: a[1]
#To get the required value min function is declared
minimum_list = min(list1, key=num)
#To get the required value max function is declared
maximum_list = max(list1, key=num)
#print function to get the output
print("Minimum value in the input:", minimum_list)
print("Maximum value in the input:", maximum_list)

Output

Minimum value in the input: (1,1)
Maximum value in the input: (4,4)

Conclusion

In this article, two types of data structures are used which are list and tuple ones. The list is initially declared and the tuple data structure is defined inside the list one. The approaches which are used to find the maximum and minimum values in a list of tuples are the min(), max(), lambda function and then finally using the numpy module.

Updated on: 07-Aug-2023

782 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements