Python | Minimum element in tuple list
Last Updated :
21 Apr, 2023
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 : Using min() + generator expression This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the minimum element using min().
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = min ( int (j) for i in test_list for j in i)
print ( "The Minimum element of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Minimum element of list is : 1
Time Complexity: O(n), where n is the length of the input list. This is because we’re using min() + generator expression which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #2 : Using min() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of finding minimum is done by combination of map() and from_iterable().
Python3
from itertools import chain
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = min ( map ( int , chain.from_iterable(test_list)))
print ( "The Minimum element of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Minimum element of list is : 1
Method #3 : Using reduce() + lambda function
Another method to find the minimum element in a tuple list is by using reduce() function from the functools module and a lambda function. The reduce function applies the lambda function to the elements of the list in a cumulative manner, and returns the final result which is the minimum element.
Python3
from functools import reduce
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = reduce ( lambda x,y: min (x,y), (j for i in test_list for j in i))
print ( "The Minimum element of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Minimum element of list is : 1
Time complexity of this approach is O(n) where n is the number of elements in the tuple list.
Auxiliary Space is O(1) as we are only storing the final result in a variable and not creating any new data structures.
Method #4 : Using a for loop:
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
min_value = float ( 'inf' )
for tup in test_list:
for val in tup:
if val < min_value:
min_value = val
print ( "The Minimum element of list is : " + str (min_value))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Minimum element of list is : 1
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Method #5 : Using extend(),list(),min() methods
Approach
- Convert list of tuples test_list to a single list x using for loop, list(),extend() methods
- Use res to store the minimum value of x
- Display res
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.extend( list (i))
res = min (x)
print ( "The Minimum element of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Minimum element of list is : 1
Time Complexity : O(N) N -length of test_list
Auxiliary Space : O(1) because we are using single variable res to store output
Share your thoughts in the comments
Please Login to comment...