We can have an application for finding the lists with the maximum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development.
Method #1 : Using reduce() + lambda
The above two functions can help us achieve this particular task. The lambda function does the task of logic and iteration and reduce function does the task of returning the required result. Works in Python 2 only.
# Python code to demonstrate # maximum sum sublist # using reduce() + lambda # importing functools for reduce() import functools # initializing matrix test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]] # printing the original matrix print ("The original matrix is : " + str(test_matrix)) # using reduce() + lambda # maximum sum sublist res = functools.reduce(lambda i, j: i if sum(i) > sum(j) else j, test_matrix) # printing result print ("Maximum sum sublist is : " + str(res)) |
The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]] Maximum sum sublist is : [4, 5, 3]
Method #2 : Using max() + key
The max function can get the maximum of all the list and key is used to specify on what the max condition has to be applied that is summation in this case.
# Python3 code to demonstrate # maximum sum sublist # using max() + key # initializing matrix test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]] # printing the original matrix print ("The original matrix is : " + str(test_matrix)) # using max() + key # maximum sum sublist res = max(test_matrix, key = sum) # printing result print ("Maximum sum sublist is : " + str(res)) |
The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]] Maximum sum sublist is : [4, 5, 3]
Recommended Posts:
- Python | Find maximum value in each sublist
- Python | Find the sublist with maximum value in given nested list
- Python - Sublist Maximum in custom sliced List
- Python - Incremental Sublist Sum
- Python | Sort a list according to the second element in sublist
- Python | Shift sublist in list
- Python | Indexing a sublist
- Python | Getting sublist element till N
- Python | Remove sublists that are present in another sublist
- Python | Split Sublist Strings
- Python | Get last element of each sublist
- Python | Get first element of each sublist
- Python | Multiply each element in a sublist by its index
- Python | Replace sublist with other in list
- Python | Convert nested sublist into tuples
- Python | Mean of consecutive Sublist
- Python - Check for Sublist in List
- Python - Replace sublist from Initial element
- Python - Count frequency of Sublist in given list
- Python | Maximum sum of elements of list in a list of lists
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.
Improved By : epskvv

