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
# 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))
OutputThe 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))
OutputThe 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
OutputThe 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.
OutputThe 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
# 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))
OutputThe 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
Similar Reads
Python | Maximum 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 maximum 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: U
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 indices
Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of minimum element of list. This task is easy and discussed many times. But sometimes, we can have multiple minimum elements and hence multiple minimum positions. Letâs discuss a shorthand to ac
3 min read
Python - Add K to Minimum element in Column Tuple List
Sometimes, while working with Tuple records, we can have a problem in which we need to perform task of adding certain element to max/ min element to each column of Tuple list. This kind of problem can have application in web development domain. Let's discuss a certain way in which this task can be p
8 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 - Maximum and Minimum K elements in Tuple
Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
8 min read
Python - Alternate Minimum element in list
Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometime
4 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
Python | Row with Minimum element in Matrix
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Update Each Element in Tuple List - Python
The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read