Python - Minimum in tuple list value


The problem at hand is to find the minimum value from the given tuple list using python programming language. So basically whenever we talk about finding the minimum value we use the min function defined in the built-in function of Python. In this article we will see different approaches to get the minimum value from the tuple list.

What is the tuple list or list of tuples?

The tuple list or we can also say list of tuples, is a data structure in Python programming. Tuple list contains multiple tuples as its items. Or we can say that every element inside the list is a tuple. For better understanding we can see the below example −

list_of_tuple = [('x', 4), (‘y', 8), ('z', 12)]

So here we have seen what a tuple list and how it looks like in python. The above example holds three tuples. Inside every tuple there are two items present one's character and the other one is an integer number. We can access these items using indexing. For example −

print(list_of_tuple[1])
#Output  ('y', 8)

So with the help of tuple list we can perform operations of both tuple and list, which is a convenient way to manipulate and store the data.

Understanding the Problem

The problem statement is to find the minimum values from the given tuple list. As we have seen what exactly tuple list is so to understand the given problem we will see an example. Let’s say we are given a tuple list as follows - [('w', [2,4]), ('x', [4,8]), ('y', [8,12]), (‘z’, [1,2])], the minimum values in this example will be - [('w’, 2), ('x', 4), ('y', 8), (‘z’,1)].

Using for loop and min function

In this method we will be using the for loop and min function to find the minimum items from the given tuple list. In this approach we will create a tuple list which will contain a list and inside this list we will be having tuples. And we will initialize an empty array to store the result. Then with the help of a loop we will traverse over each list and find the minimum number and then add or append it in the array we have declared initially.

Algorithm

  • Step 1 − So first we will define a tuple list and give it a name as tupleList. This variable will contain the list and inside this list there are multiple tuples. And in each tuple there is another list containing. And our task is to find the minimum value within this list.

  • Step 2 − After the above step we will create an object to contain the resultant tuple list and give it a name as min_values.

  • Step 3 − Then we will initialize a loop to iterate the tuple list. In this loop we will use the min function for every list inside the tuple and store the minimum value in min_val.

  • Step 4 − After that we will append all the min_val with the help of the append method in the min_values object. And then print the value of min_values.

Example

tupleList = [('X', [14, 12, 25]), ('Y', [21, 32, 24]), ('Z', [45, 30, 16])]

# Find the minimum value in each list
min_values = []
for k, l in tupleList:
   min_val = min(l)
   min_values.append((k, min_val))

#The input tuple list
print("Input tuple list: " + str(tupleList))
# printing the minimum values
print("The minimum values are: " + str(min_values))

Output

Input tuple list: [('X', [14, 12, 25]), ('Y', [21, 32, 24]), ('Z', [45, 30, 16])]
The minimum values are: [('X', 12), ('Y', 21), ('Z', 16)]

Complexity

The time complexity using this approach to find the minimum values from the given tuple list is O(n*m), here n is the length of the given tupleList and m is the maximum size of the lists inside the tuples.

Using min and lambda function

In this approach we will be using two built-in functions of the Python programming language first is min and second is lambda function. So in the first step define a function to find the minimum values from the given tuple list. After that we will use the min function to get the minimum value. And then using a lambda function which is an anonymous function to define one line function without giving the name of the function.

Algorithm

  • Step 1 − First we will define a function using the def keyword and give this function a name as getMinValue and in this function we will take a parameter of tuple list as t_list.

  • Step 2 − After defining the function we will store the minimum value in the min_item variable and initialize its value using min function and lambda function. Inside the lambda function we will define the key for each value in the tuple list.

  • Step 3 − Now we will return the minimum value found in step two. And print the desired value.

Example

#Function to find the minimum value from tuple list
def getMinValue(t_list):
   min_item = min(t_list, key=lambda x: x[0])
   return min_item[0]

#initialize the tuple list
t_list = [(12, 18), (16, 13), (14, 19)]
min_item = getMinValue(t_list)
print(f"Minimum value in the given tuple list is: {min_item}")

Output

Minimum value in the given tuple list is: 12

Complexity

The time complexity using built-in functions like min and lambda to find the minimum value from the tuple list is O(n), in this n is the size of the given input tuple list. As we have used the min function to iterate through the items of the tuple list and accessing the minimum value from the list requires n time.

Conclusion

The conclusion is that we have successfully implemented the code to get the minimum values from the tuple list using different approaches and saw the time complexity for all the approaches. And we have also seen the usage of lambda function in this problem.

Updated on: 16-Oct-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements