Open In App

Python | Maximum sum of elements of list in a list of lists

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python program to find the
# list in a list of lists whose
# sum of elements is the highest
# using traversal
 
def maximumSum(list1):
    maxi = 0
 
    # traversal in the lists
    for x in list1:
        sum = 0
        # traversal in list of lists
        for y in x:
            sum+= y    
        maxi = max(sum, maxi)
         
    return maxi
     
# driver code 
list1 = [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
print maximumSum(list1)


Output

33

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




# Python program to find the
# list in a list of lists whose
# sum of elements is the highest
# using sum and max function and traversal
 
def maximumSum(list1):
    maxi = 0
    # traversal
    for x in list1:
        maxi = max(sum(x), maxi)
         
    return maxi
     
 
# driver code 
list1 = [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
print maximumSum(list1)


Output

33

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




# Python program to find the
# list in a list of lists whose
# sum of elements is the highest
# using sum and max function
 
def maximumSum(list1):
    return(sum(max(list1, key = sum)))
     
 
# driver code 
list1 = [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
print maximumSum(list1)


Output

33

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




# Python program to find the
# list in a list of lists whose
# sum of elements is the highest
# using traversal
 
def maximumSum(list1):
    x=[]
    for i in list1:
        x.append(sum(i))
    x.sort()
    return x[-1]
         
     
# driver code
list1 = [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
print(maximumSum(list1))


Output

33

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):
    # Use the reduce function to calculate the maximum sum
    # by comparing the sums of each list
    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))  # 33
#This code is contributed by Edula Vinay Kumar Reddy


Output

33

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)



Similar Reads

Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python | Ways to sum list of lists and return sum list
The list is an important container and is used almost in every code of day-day programming as well as web development, more it is used, the more is the requirement to master it and hence knowledge of its operations is necessary. Given a list of lists, the program to suppose to return the sum as the final list. Let's see some of the methods to sum a
5 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
Indexing Lists Of Lists In Python
Lists of lists are a common data structure in Python, providing a versatile way to organize and manipulate data. When working with nested lists, it's crucial to understand how to index and access elements efficiently. In this article, we will explore three methods to index lists of lists in Python using the creation of a sample list, followed by ex
3 min read
Python | Sorting list of lists with similar list elements
Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Let's discuss certain ways in which this can be perf
5 min read
Python | Merge List with common elements in a List of Lists
Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27, 55], [43, 0, 22], [43, 0, 96], [55, 27, 11]] Out
3 min read
Python | Get positive elements from given list of lists
Given a list of list, the task is to get only positive element from given list. Below are some ways to solve the above problem. Method #1: Using Iteration C/C++ Code # Python code to get positive # element from list of list # List Initialisation Input = [[10, -11, 222], [42, -222, -412, 99, -87]] Output = [] # Using iteration for elem in Input: tem
5 min read
Python | Find common elements in list of lists
The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists. Let's discuss certain ways in which this operation can be performed. Method #1 : Using reduce() + lambda + s
6 min read
Python | Convert column to separate elements in list of lists
There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let's discuss certain ways in which this action can be performed.Method #1 : Using list slicing and list comprehension The functionality of list sl
4 min read
Practice Tags :
three90RightbarBannerImg