Open In App

Python | Minimum element in tuple list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Minimum element in tuple list
# using min() + generator expression
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Minimum element in tuple list
# using min() + generator expression
res = min(int(j) for i in test_list for j in i)
 
# printing result
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




# Python3 code to demonstrate working of
# Minimum element in tuple list
# using min() + map() + chain.from_iterable()
from itertools import chain
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Minimum element in tuple list
# using min() + map() + chain.from_iterable()
res = min(map(int, chain.from_iterable(test_list)))
 
# printing result
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
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Minimum element in tuple list
# using reduce() + lambda function
res = reduce(lambda x,y: min(x,y), (j for i in test_list for j in i))
 
# printing result
print("The Minimum element of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# find minimum value
min_value = float('inf')
for tup in test_list:
    for val in tup:
        if val < min_value:
            min_value = val
# printing result
print("The Minimum element of list is : " + str(min_value))
#This code is contributed by Jyothi pinjala.


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

  1. Convert list of tuples test_list to a single list x using for loop, list(),extend() methods
  2. Use res to store the minimum value of x
  3. Display res

Python3




# Python3 code to demonstrate working of
# Minimum element in tuple list
 
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Minimum element in tuple list
x=[]
for i in test_list:
    x.extend(list(i))
res=min(x)  
# printing result
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



Last Updated : 21 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads