Open In App

Python – Minimum in tuple list value

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 minimum of list as tuple attribute. Let’s discuss certain ways in which this task can be performed.

Method #1: Using sort() method + for loop






# Python3 code to demonstrate
# Minimum in tuple list value
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
 
# Minimum of list as tuple attribute
res=[]
for i in test_list:
    a=i[1]
    a.sort()
    res.append((i[0],a[0]))
# print result
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 (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M))



Method #2: 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 minimum of the list as a tuple attribute and list comprehension to iterate through the list. 




# Python3 code to demonstrate
# Minimum in tuple list value
# using list comprehension + min()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + min()
# Minimum of list as tuple attribute
res = [(key, min(lst)) for key, lst in test_list]
 
# print result
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 (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M))

Method #3: 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 code to demonstrate
# Minimum in tuple list value
# using map() + lambda + min()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + lambda + min()
# Minimum in tuple list value
res = list(map(lambda x: (x[0], min(x[1])), test_list))
 
# print result
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 (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M)), where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.

Method 4: Using for loop + min()

Below is the implementation:




# Python code to find the minimum value in a list of tuples
 
# initializing list of tuples
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing the original list
print("The original list is: " + str(test_list))
 
# using a for loop to find the minimum value in each list
# and store it as a tuple with the corresponding key
min_values = []
for key, lst in test_list:
    min_val = min(lst)
    min_values.append((key, min_val))
 
# printing the list of tuples with the minimum values
print("The list of tuples with the minimum values is: " + str(min_values))

Output
The original list is: [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list of tuples with the minimum values is: [('key1', 3), ('key2', 1), ('key3', 3)]

Time Complexity: O(n * k), where n is the number of tuples in the list and k is the length of the largest list in the tuples. 
Auxiliary Space: O(n), where n is the number of tuples in the list.


Article Tags :