Python | Maximize Record list
Last Updated :
01 May, 2023
Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization 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() 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 maximization across lists is performed with help of zip().
Python3
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
res = [( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ])) for x, y in zip (test_list1, test_list2)]
print ("The Maximums across lists is : " + str (res))
|
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension + zip() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using max() + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that maximization is performed by inbuilt function and extending logic to each element is done by map().
Python3
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
res = [ tuple ( map ( max , zip (a, b))) for a, b in zip (test_list1, test_list2)]
print ("The maximums across lists is : " + str (res))
|
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 : use the itertools.starmap() function to find the maximums across the lists. This function applies the provided function to the elements of the input iterable(s) and returns an iterator yielding the results.
Here is an example:
Python3
from itertools import starmap
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " , test_list1)
print ( "The original list 2 : " , test_list2)
res = list (starmap( lambda x, y: ( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ])), zip (test_list1, test_list2)))
print ( "The Maximums across lists is : " , res)
|
Output
The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Maximums across lists is : [(5, 4), (8, 10), (8, 14)]
This approach is unique as it uses the itertools.starmap() function to apply the max function to the elements of the input iterable(s) and returns an iterator yielding the results. Time and Auxiliary Space is O(n) where n is the length of the lists.
Method #4: using map() and lambda:
Python3
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " , test_list1)
print ( "The original list 2 : " , test_list2)
res = list ( map ( lambda x, y: tuple ( map ( max , x, y)), test_list1, test_list2))
print ( "The maximums 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 maximums across lists is : [(5, 4), (8, 10), (8, 14)]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using a for loop
Step-by-step approach:
- Initialize an empty list called res.
- Use a for loop to iterate over the indices of test_list1 and test_list2 simultaneously.
- For each iteration, find the maximum value between the corresponding tuples in test_list1 and test_list2 using the max() function.
- Append the resulting tuple to the res list.
- After all iterations are complete, print the resulting list res.
Python3
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for i in range ( len (test_list1)):
max_tuple = ( max (test_list1[i][ 0 ], test_list2[i][ 0 ]), max (test_list1[i][ 1 ], test_list2[i][ 1 ]))
res.append(max_tuple)
print ( "The Maximums 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 Maximums across lists is : [(5, 4), (8, 10), (8, 14)]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #6: Using NumPy
Explanation:
- First, we import the NumPy library.
- Then, we convert the lists to NumPy arrays using the np.array() method.
- We then perform the element-wise maximum operation using the np.maximum() method.
- Finally, we convert the resulting NumPy array back to a list using the tolist() method and print the result.
Python3
import numpy as np
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
res = np.maximum(arr1, arr2)
print ( "The Maximums across lists is : " + str (res.tolist()))
|
Output:
The Maximums across lists is : [[5, 4], [8, 10], [8, 14]]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Similar Reads
Python | Maximize Column in Records List
Sometimes, we encounter a problem where we deal with a complex type of matrix column maximization in which we are given a tuple and we need to perform the maximization of its like elements. This has a good application in the Machine Learning domain. Letâs discuss certain ways in which this can be do
4 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
Python - Maximum Sum Record
Sometimes, while working with data, we might have a problem in which we need to find maximum sum between available pairs in list. This can be application to many problems in mathematics domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using max() + list comprehensi
3 min read
Python - Equable Minimial Records
Sometimes, while working with Python records, we can have a problem in which we need to extract one of the records that are equal to certain index, which is minimal of other index. This kind of problem occurs in domains such as web development. Let's discuss certain ways in which this task can be pe
5 min read
Python - Row with Maximum Record Element
Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Python - Maximum in Row Range
Given a range and a Matrix, extract the maximum element out of that range of rows. Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5 Output : 12 Explanation : Checks for rows 2, 3 and 4, maximum element is 12. Input : test_list = [[4, 3, 6], [9, 1
5 min read
Python - Nested Records List from Lists
Sometimes, while working with Python Data, we can have problems in which we have data incoming in different formats. In this, we can receive data as key and value in separate dictionaries and we are required to make values as list of records with a new key. Let's discuss certain ways in which we can
6 min read
Python - Maximum Quotient Pair in List
Sometimes, we need to find the specific problem of getting the pair which yields the maximum Quotient, this can be solved by sorting and getting the first and last elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in a similar list withou
5 min read
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 | Maximize alternate element List
The problem of getting maximum of a list is quite generic and we might some day face the issue of getting the maximum of alternate elements and get the list of 2 elements containing maximum of alternate elements. Letâs discuss certain ways in which this can be performed. Method #1 : Using list compr
3 min read