Related Articles

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

• Last Updated : 21 Nov, 2018

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 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
```

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 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
```

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 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
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up