Python | Maximum sum of elements of list in a list of lists
Last Updated :
11 Jan, 2023
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples:
Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
Output : 33
Explanation: sum of all lists in the given list of lists are:
list1 = 6, list2 = 15, list3 = 33, list4 = 24
so the maximum among these is of
Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]]
Output : 12
Method 1 : Traversal of list in lists
We can traverse in the lists inside the list and sum up all the elements in a given list and by max function get the maximum of sum of all elements in lists of list.
Python
def maximumSum(list1):
maxi = 0
for x in list1:
sum = 0
for y in x:
sum + = y
maxi = max ( sum , maxi)
return maxi
list1 = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]
print maximumSum(list1)
|
Time Complexity: O(n*m) where n is the number of lists and m is the maximum size of the list.
Auxiliary Space: O(1)
Method 2 : Traversal of list
Traverse in the outer list only, and sum all elements in the inner lists by using sum() function, find the sum of all the lists and get the maximum of all the sum calculated.
Python
def maximumSum(list1):
maxi = 0
for x in list1:
maxi = max ( sum (x), maxi)
return maxi
list1 = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]
print maximumSum(list1)
|
Time Complexity: O(n*m) where n is the number of lists and m is the maximum size of the list.
Auxiliary Space: O(1)
Method 3 : Sum and Max function
sum(max(list1, key=sum))
The above syntax of max() function allows us to find the sum of list in list using the key=sum. max(list1, key=sum), this finds the list with maximum sum of elements and then sum(max(list1, key=sum)) returns us the sum of that list.
Python
def maximumSum(list1):
return ( sum ( max (list1, key = sum )))
list1 = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]
print maximumSum(list1)
|
Time Complexity: O(n*m) where n is the number of lists and m is the maximum size of the list.
Auxiliary Space: O(1)
Method 4 : Using sum() and sort() methods
Python3
def maximumSum(list1):
x = []
for i in list1:
x.append( sum (i))
x.sort()
return x[ - 1 ]
list1 = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]
print (maximumSum(list1))
|
Time Complexity : O(NlogN)
Auxiliary Space : O(N)
Method 5 : Using reduce()
This method uses the reduce() function to iterate over the list of lists and a lambda function to compare the sums of each list. The lambda function returns the list with the larger sum, and the reduce function keeps track of the maximum sum by repeatedly applying the lambda function to the list of lists. The final result is the sum of the list with the maximum sum.
Here is another approach using the reduce() function and a lambda function:
Python3
from functools import reduce
def maximum_sum(lists):
max_sum = reduce ( lambda x, y: x if sum (x) > sum (y) else y, lists)
return sum (max_sum)
lists = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]
print (maximum_sum(lists))
|
Time complexity: O(n*m) where n is the number of lists and m is the maximum size of the list.
Space complexity: O(1)
Please Login to comment...