Open In App

Python – Minimum in tuple list value

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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




# 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()

  • Create an empty list called min_values that we will use to store the tuples with the minimum values.
  • Use a for loop to iterate through each tuple in the list of tuples. We will use the variable name tup to represent the current tuple, and unpack it into the variables key and lst.
  • Inside the for loop, use the built-in min() function to find the minimum value in the list associated with the current key.
  • Create a new tuple that consists of the key and the minimum value that we just found.
  • Append the new tuple to the min_values list.
  • After the for loop has finished iterating through all the tuples in the list, print out the resulting list of tuples that have the minimum values.

Below is the implementation:

Python3




# 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads