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 = [[(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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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 #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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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)]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.