Python - Maximum sum of elements of list in a list of lists Last Updated : 15 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In this problem, we need to find the maximum sum of elements in a list of lists. A list of lists is a collection of sub-lists and each sub-list contains individual elements. The problem is to find the maximum sum among all the sub-lists. Let's explore various ways to solve this problem.Using sum() with max()The simplest and most efficient way is by using the sum() function to calculate the sum of elements in each sub-list and then using the max() function to find the maximum sum. Python # Input list of lists lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Find the maximum sum of elements in the sub-lists res = max(sum(sub_lst) for sub_lst in lst) print(res) Output24 Explanation:sum() function is used to calculate the sum of elements in each sub-list.max() function is then used to find the sub-list with the highest sum.Let's explore some more ways and see how we can find the maximum sum of elements of list in a list of lists.Table of ContentUsing for loopUsing list comprehension and max()Using numpy for large listsUsing for loopWe can also use a for loop to iterate through each sub-list, calculate the sum of each sub-list and then keep track of the maximum sum encountered during the iteration. Python # Input list of lists a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Initialize the maximum sum as a very small number max_sum = float('-inf') # Loop through each sub-list and calculate the sum for b in a: cur_sum = sum(b) max_sum = max(max_sum, cur_sum) print(max_sum) Output24 Explanation:We use a for loop to calculate the sum of each sub-list using the sum() function.We keep track of the maximum sum encountered so far using the max() function.Using list comprehension and max()This method is similar to the first one, but we will use list comprehension to create a list of sums and then apply the max() function to find the maximum sum. Python a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Create a list of sums and find the maximum sum res = max([sum(sub_lst) for sub_lst in a]) print(res) Output24 Explanation:List comprehension is used to create a list of sums for each sub-list.max() function is then used to find the maximum sum from the list of sums.Using numpy for large listsIf we are working with large lists, the numpy library can provide an efficient solution for this problem. Using numpy's sum() function, we can find the maximum sum of elements in the list of lists. Python import numpy as np a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Convert the list of lists to a numpy array arr = np.array(a) # Find the maximum sum of elements in a row (sub-list) res = np.max(np.sum(arr, axis=1)) print(res) Output24 Explanation:We first convert the list of lists into a numpy array.The np.sum() function calculates the sum of each row (sub-list) and np.max() finds the maximum sum. Comment More infoAdvertise with us Next Article Python - Maximum sum of elements of list in a list of lists S Striver Follow Improve Article Tags : Misc Python python-list Python list-programs Practice Tags : Miscpythonpython-list Similar Reads Python | Find frequency of largest element in list Given a list, the task is to find the number of occurrences of the largest element of the list.Examples: Input : [1, 2, 8, 5, 8, 7, 8] Output :3 Input : [2, 9, 1, 3, 4, 5] Output :1 Method 1: The naive approach is to find the largest element present in the list using max(list) function, then iterati 2 min read Longest alternating subsequence which has maximum sum of elements Given a list of length N with positive and negative integers. The task is to choose the longest alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite of the sign of the current element). Among all such subsequences, we have to choose one which has the maxi 10 min read Python Program to Find Largest Element in an Array To find the largest element in an array, iterate over each element and compare it with the current largest element. If an element is greater, update the largest element. At the end of the iteration, the largest element will be found. Given an array, find the largest element in it. Input : arr[] = {1 4 min read Python | Max/Min value in Nth Column in Matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed 7 min read Element with Largest Frequency in List We are given a list we need to find the element with largest frequency in a list . For example, we are having a list a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to find the element with most frequency which will be 4 in this case.Using collections.CounterCounter is a convenient way to count elements 3 min read Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu 4 min read Python - Find maximum value in each sublist Finding the maximum value in each sublist involves iterating through a list of lists and identifying the largest element within each sublist. This operation is useful for analyzing data sets, and extracting the most significant values from grouped information. In this article, we will explore method 2 min read Python map function to find row with maximum number of 1's Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s. Examples: Input: matrix = [[0, 1, 1, 1], [0, 0, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]] Output: 2 We have existing solution for this problem please refer Find the row with maximum number of 1's. We can solve t 1 min read Python | sort list of tuple based on sum Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple. Examples: Input: [(4, 5), (2, 3), (6, 7), (2, 8)] Output: [(2, 3), (4, 5), (2, 8), (6, 7)] Input: [(3, 4), (7, 8), (6, 5)] Output: [(3, 4), (6, 5), (7, 8)] # Method 1: Using bubble sort Using th 4 min read Largest, Smallest, Second Largest, Second Smallest in a List-Python We are given a list of numbers and our task is to find the largest, smallest, second largest and second smallest elements in the list. For example, if the list is [4, 1, 7, 3, 9], the largest is 9, the smallest is 1, the second largest is 7 and the second smallest is 3. Let's explore different ways 4 min read Like