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
# Python3 code to demonstrate
# Record Value list Minimum
# 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()
# Record Value list Minimum
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(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
# Python3 code to demonstrate
# Record Value list Minimum
# 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()
# Record Value list Minimum
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(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
# Using reduce
from functools import reduce
# Initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
# Finding minimum in each sublist using reduce
result = [(key, reduce(lambda x, y: x if x<y else y, lst)) for key, lst in test_list]
# Printing the final result
print("The list tuple attribute minimum is:", result)
OutputThe 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
# Python3 code to demonstrate
# Record Value list Minimum
# using for loop
# 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 for loop
# Record Value list Minimum
res = []
for key, lst in test_list:
min_val = min(lst)
res.append((key, min_val))
# 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), 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
# Python3 code to demonstrate
# Record Value list Minimum
# using dictionary comprehension
# 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 dictionary comprehension
# Record Value list Minimum
res_dict = {key: min(lst) for key, lst in test_list}
res = list(res_dict.items())
# 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)
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
# 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 itertools.chain() and min()
# Record Value list Minimum
res = []
for key, lst in test_list:
min_val = min(itertools.chain(*[lst]))
res.append((key, min_val))
# 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(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.
Similar Reads
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
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 certai
5 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 - Maximum value in record list as tuple attribute
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 max of a list as a tuple attribute. Letâs discuss certai
8 min read
Python | Minimum K records of Nth index in tuple list
Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using filter() + l
9 min read
Python - Maximum record value key in dictionary
Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
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 - Records Maxima in List of Tuples
Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using ma
5 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