Python – Minimum in each record value list
Last Updated :
16 May, 2023
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the min of list as tuple attribute. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + min() This particular problem can be solved using list comprehension combined with the min function in which we use min function to find the min of list as a tuple attribute and list comprehension to iterate through the list.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ("The original list : " + str (test_list))
res = [(key, min (lst)) for key, lst in test_list]
print ("The list tuple attribute minimum is : " + str (res))
|
Output :
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #2: Using map + lambda + min() The above problem can also be solved using the map function to extend the logic to the whole list and min function can perform the similar task as the above method.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ("The original list : " + str (test_list))
res = list ( map ( lambda x: (x[ 0 ], min (x[ 1 ])), test_list))
print ("The list tuple attribute minimum is : " + str (res))
|
Output :
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #3 : Using reduce()
Python3
from functools import reduce
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
result = [(key, reduce ( lambda x, y: x if x<y else y, lst)) for key, lst in test_list]
print ( "The list tuple attribute minimum is:" , result)
|
Output
The list tuple attribute minimum is: [('key1', 3), ('key2', 1), ('key3', 3)]
Time complexity: O(n^2)
Space complexity: O(n)
Explanation:
In this approach, we use the reduce function from the functools library to find the minimum in each sublist. We iterate through the list of tuples and use the reduce function to find the minimum of each sublist. Finally, we store the result in a new list of tuples and print the result.
Method 4: Use a loop to iterate through the list of tuples and finding the minimum value for each tuple’s value list.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ( "The original list : " + str (test_list))
res = []
for key, lst in test_list:
min_val = min (lst)
res.append((key, min_val))
print ( "The list tuple attribute minimum is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]
Time Complexity: O(n * m), where n is the length of the list and m is the length of the longest value list.
Auxiliary Space: O(n), where n is the length of the list.
Method 5: Uses a dictionary comprehension
This method uses a dictionary comprehension to create a dictionary where the keys are the first elements of the tuples and the values are the minimum values of the second elements of the tuples. Then, it converts the dictionary to a list of tuples.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ( "The original list : " + str (test_list))
res_dict = {key: min (lst) for key, lst in test_list}
res = list (res_dict.items())
print ( "The list tuple attribute minimum is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]
Time complexity: O(n)
Auxiliary space: O(n)
Method #6: Using itertools.chain() and min()
- Import itertools module.
- Use itertools.chain() function to flatten the list of lists into a single list.
- Use min() function to find the minimum value in the flattened list.
- Repeat the above steps for each list of tuples in the given list and store the minimum value in a list.
- Create a new list of tuples with keys from the original list and minimum values from the list created in step 4.
Python3
import itertools
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ( "The original list : " + str (test_list))
res = []
for key, lst in test_list:
min_val = min (itertools.chain( * [lst]))
res.append((key, min_val))
print ( "The list tuple attribute minimum is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]
Time complexity: O(nm) where n is the number of tuples in the list and m is the length of the longest list in the tuples.
Auxiliary space: O(nm) to store the flattened list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...