Open In App

Python | Maximize Record list

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Maximize Record list
# using list comprehension + zip()
 
# 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))
 
# Maximize Record list
# using list comprehension + zip()
res = [(max(x[0], y[0]), max(x[1], y[1])) for x, y in zip(test_list1, test_list2)]
 
# printing result
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




# Python3 code to demonstrate working of
# Maximize Record list
# using max() + 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))
 
# Maximize Record list
# using max() + zip() + map()
res = [tuple(map(max, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
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
 
# 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 : ", test_list1)
print("The original list 2 : ", test_list2)
 
# Maximize Record list
res = list(starmap(lambda x, y: (max(x[0], y[0]), max(x[1], y[1])), zip(test_list1, test_list2)))
 
# printing result
print("The Maximums across lists is : ", res)
#This code is contributed by Edula Vinay Kumar Reddy


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)]
# printing original lists
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))
#This code is contributed by Jyothi pinjala


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




# Python3 code to demonstrate working of
# Maximize Record list
# using a for loop
 
# 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))
 
# Maximize Record list using a for loop
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)
 
# printing result
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
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# convert lists to NumPy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
 
# perform element-wise maximum operation
res = np.maximum(arr1, arr2)
 
# printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads