Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | List of tuples Minimum

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() + min() The combination of above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the minimum across lists is performed with help of zip(). The minimum is performed using min(). 

Python3




# Python3 code to demonstrate working of
# List of tuples Minimum
# using list comprehension + zip() + min()
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# List of tuples Minimum
# using list comprehension + zip() + min()
res = [(min(x[0], y[0]), min(x[1], y[1])) for x, y in zip(test_list1, test_list2)]
 
# printing result
print("The Minimum across lists is : " + str(res))


Output : 

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Minimum across lists is : [(2, 4), (6, 7), (5, 1)]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

  Method #2 : Using min() + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that minimum is performed by inbuilt function and extending logic to each element is done by map(). 

Python3




# Python3 code to demonstrate working of
# List of tuples Minimum
# using min() + zip() + map()
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# List of tuples Minimum
# using min() + zip() + map()
res = [tuple(map(min, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The Minimum across lists is : " + str(res))


Output : 

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Minimum across lists is : [(2, 4), (6, 7), (5, 1)]

Approach 3: Using numpy library

Note: Install numpy module using command “pip install numpy”

Python3




# Approach 3: Using numpy library
 
import numpy as np
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# converting lists to numpy arrays
np_arr1 = np.array(test_list1)
np_arr2 = np.array(test_list2)
 
# List of tuples Minimum using numpy library
res = np.minimum(np_arr1, np_arr2)
 
# printing result
print("The Minimum across lists is : " + str(res.tolist()))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The Minimum across lists is : [(2, 4), (6, 7), (5, 1)]

Time complexity: O(n) where n is the number of tuples.
Auxiliary Space: O(n) where n is the number of tuples.

In this approach, we use the numpy library to perform the task. The numpy.minimum function is used to perform element-wise minimum comparison between two numpy arrays. 

Using a for loop:

Approach:

  • We loop over each tuple in both lists using a range function.
  • We create a new tuple by getting the minimum values from the corresponding tuples in each list using the min function.
  • We append the new tuple to the minimum list.
  • Finally, we print the minimum list containing the minimum tuples from both lists.

Python3




list1 = [(2, 4), (6, 7), (5, 1)]
list2 = [(5, 4), (8, 10), (8, 14)]
 
minimum = []
 
for i in range(len(list1)):
    minimum.append((min(list1[i][0], list2[i][0]), min(list1[i][1], list2[i][1])))
 
print("Minimum across lists is:", minimum)


Output

Minimum across lists is: [(2, 4), (6, 7), (5, 1)]

Time Complexity: O(n^2)
Space Complexity: O(1)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments