Python - Minimum in tuple list value
Last Updated :
09 Apr, 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 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Minimum in each record value list 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 wa
6 min read
Python - Column Minimum in Tuple list Sometimes, while working with records, we can have a problem in which we need to find min of all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using min()
6 min read
Python | Minimum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
5 min read
Python | List of tuples Minimum Sometimes, while working with Python records, we can have a problem in which we need to perform cross minimum of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip()
4 min read
Min and Max value in list of tuples-Python The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python | Index minimum value Record In Python, we can bind structural information in the form of tuples and then can retrieve the same, and has manyfold applications. But sometimes we require the information of a tuple corresponding to a minimum value of another tuple index. This functionality has many applications such as ranking. Le
4 min read
Maximum and Minimum value from two lists - Python Finding the maximum and minimum values from two lists involves comparing all elements to determine the highest and lowest values. For example, given two lists [3, 5, 7, 2, 8] and [4, 9, 1, 6, 0], we first examine all numbers to identify the largest and smallest. In this case, 9 is the highest value
3 min read
Minimum Value Keys in Dictionary - Python We are given a dictionary and need to find all the keys that have the minimum value among all key-value pairs. The goal is to identify the smallest value in the dictionary and then collect every key that matches it. For example, in {'a': 3, 'b': 1, 'c': 2, 'd': 1}, the minimum value is 1, so the res
4 min read
Python - Find minimum k records from tuple list Sometimes, while working with data, we can have a problem in which we have records and we require to find the lowest K scores from it. This kind of application is popular in web development domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using sorted() + lambda Th
6 min read
Python - Minimum value key assignment Given two dictionaries, the task is to write a Python program to assign minimum values for matching keys from both dictionaries. Examples: Input : test_dict1 = {"gfg" : 1, "is" : 7, "best" : 8}, test_dict2 = {"gfg" : 2, "is" : 2, "best" : 10}Output : {"gfg" : 1, "is" : 2, "best" : 8}Explanation : Mi
5 min read