Open In App

Python – Row with Maximum Record Element

Improve
Improve
Like Article
Like
Save
Share
Report

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 = [[(70, 4), (6, 7)], [(15, 2), (19, 3)]] 
Output : [(70, 4), (6, 7)] 

Input : test_list = [[(20, 4)], [(15, 2)], [(34, 6)]] 
Output : [(34, 6)]

Method #1 : Using max() + key The combination of above functions can be used to solve this problem. In this, we extract maximum row using max() and key is used to check for initial element of records. 

Python3




# Python3 code to demonstrate working of
# Row with Maximum Record Element
# Using max() + key
 
# initializing list
test_list = [[(12, 4), (6, 7)],
             [(15, 2), (19, 3)],
             [(18, 3), (12, 4)],
             [(17, 1), (11, 3)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Row with Maximum Record Element
# Using max() + key
res = max(test_list, key = max)
 
# printing result
print("The row with Maximum Value : " + str(res))


Output : 

The original list is : [[(12, 4), (6, 7)], [(15, 2), (19, 3)], [(18, 3), (12, 4)], [(17, 1), (11, 3)]] The row with Maximum Value : [(15, 2), (19, 3)]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The max() + key is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

  Method #2 : Using max() + itemgetter() This is yet another way to solve this problem. This approach provides flexibility to make a choice on element index for comparison, rather than just initial as in above method by the usage of itemgetter(). 

Python3




# Python3 code to demonstrate working of
# Row with Maximum Record Element
# Using max() + itemgetter()
from operator import itemgetter
 
# initializing list
test_list = [[(12, 4), (6, 7)],
             [(15, 2), (19, 3)],
             [(18, 3), (12, 4)],
             [(17, 1), (11, 3)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Row with Maximum Record Element
# Using max() + itemgetter()
res = max(test_list, key = itemgetter(1))
 
# printing result
print("The row with Maximum Value : " + str(res))


Output : 

The original list is : [[(12, 4), (6, 7)], [(15, 2), (19, 3)], [(18, 3), (12, 4)], [(17, 1), (11, 3)]] The row with Maximum Value : [(15, 2), (19, 3)]

Method #3 : Using max() + lambda function

In this approach, we use the max() function to find the sublist with maximum record element in the given list of sublists test_list. We pass a lambda function to max() as the key function to calculate the sum of the record elements in each sublist.

The sum(t[0] for t in x) expression in the lambda function calculates the sum of the first element in each tuple in the sublist x. If you have a different definition of “maximum record element”, you can adjust the lambda function accordingly.

The max_sublist variable will contain the sublist with maximum record element. We then print the result using the print() function.

This approach is similar to the previous one, but uses a more concise lambda function.

Python3




# Sample list of sublists
test_list = [[(20, 4)], [(15, 2)], [(34, 6)]]
 
# Find the sublist with maximum record element
max_sublist = max(test_list, key=lambda x: sum(t[0] for t in x))
 
# Print the sublist with maximum record element
print(max_sublist)


Output

[(34, 6)]

The time complexity of this code is O(nm), where n is the length of test_list and m is the length of the longest sublist within test_list. This is because the max() function needs to iterate over each sublist in test_list, and the lambda function used as the key needs to sum the first elements of each tuple within each sublist. The lambda function has a time complexity of O(m) for each sublist, resulting in a worst-case time complexity of O(nm) for the entire function call.

The space complexity of this code is O(m), where m is the length of the longest sublist within test_list. This is because only a single sublist is stored in memory at a time, and the space required to store the max_sublist variable is proportional to the length of the longest sublist.

Method #4 :Using heapq:

  1. Import the heapq module
  2. Create a sample list of sublists test_list with tuples inside each sublist
  3. Use the heapq.nlargest() function to find the sublist with maximum record element
  4. The function takes three arguments:
  5. The number of sublists to return (in this case 1)
  6. The list to search (test_list)
  7. The key argument specifies a function of one argument to extract a comparison key from each sublist. Here,
  8. we sum the first element of each tuple in each sublist.
  9. The heapq.nlargest() function returns a list with the requested number of sublists sorted in descending order based on the key function.
  10. In this case, we request the largest sublist, so we extract the first (and only) element of the returned list using [0].
  11. Print the resulting sublist.

Python3




import heapq
 
# Sample list of sublists
test_list = [[(20, 4)], [(15, 2)], [(34, 6)]]
# printing original list
print("The original list is : " + str(test_list))
 
# Find the sublist with maximum record element
max_sublist = heapq.nlargest(1, test_list, key=lambda x: sum(t[0] for t in x))[0]
 
# Print the sublist with maximum record element
print("The row with Maximum Value : "+str(max_sublist))
#This code is contributed by Jyothi Pinjala.


Output

The original list is : [[(20, 4)], [(15, 2)], [(34, 6)]]
The row with Maximum Value : [(34, 6)]

The time complexity : O(n log k), where n is the length of the input list and k is the number of elements to be returned. In this case, k is 1, so the time complexity is O(n log 1) = O(n).

The auxiliary space: O(k), as we are only keeping track of the k largest elements in the heap at any given time. In this case, k is 1, so the space complexity is O(1).

Method #5:Using reduce

Algorithm

  1. Initialize a variable max_sublist to the first sublist in the list.
  2. Iterate over each sublist in the list, comparing the record element sum of the current sublist with that of the max_sublist.
  3. If the record element sum of the current sublist is greater than that of max_sublist, set max_sublist equal to the current sublist.
  4. Return max_sublist.

Python3




from functools import reduce
 
# Sample list of sublists
test_list = [[(20, 4)], [(15, 2)], [(34, 6)]]
 
# Find the sublist with maximum record element using reduce()
max_sublist = reduce(lambda a, b: a if sum(t[0] for t in a) > sum(t[0] for t in b) else b, test_list)
 
# Print the sublist with maximum record element
print(max_sublist)


Output

[(34, 6)]

The time complexity of the algorithm for finding the sublist with the maximum record element in a list of sublists using iteration is O(n), where n is the number of sublists in the list. This is because the algorithm requires iterating over each sublist in the list once to compare their record element sums.

The auxiliary space of the algorithm is O(1), because the algorithm does not use any additional data structures that depend on the size of the input list. The algorithm only requires storing the current maximum sublist and the current sublist during the iteration, which are both constant-sized.



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