Change language

Python | Maximum sum of list items 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: list traversal in lists

We can move through the lists within the list and sum all the elements in the given list and use the max function to get the maximum sum of all the elements in the lists of the list.

# Python program to search
# list in the list of lists whose
# sum elements highest
# use bypass

 

de f maximumSum (list1):

maxi = 0

  

# crawl lists

for x in list1:

sum = 0  

# crawl list of lists

for y in x:

sum + =

  maxi = max ( sum , maxi) 

  

  return maxi

 
# driver code

list1 = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 11 , 12 ], [ 7 , 8 , 9 ]]

print maximumSum (list1)

Exit:

 33 

Method 2: traversing the list

Traverse only the outer list and sum all the elements in the inner lists using the

# Program mma Python to search
# list in the list of lists whose
# the sum of the elements is the highest
# using sum and max function and bypass

 

def maximumSum (list1):

maxi = 0

# workaround

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)

Exit:

 33 

Method 3: Sum and Max Function

  sum (max (list1, key = sum))  

The above function syntax max () allows us to find the sum of a list in a list using key = sum max (list1, key = sum) , this finds the list with the maximum sum of elements, and then sum (max (list1, key = sum)) returns us the sum of that list.

# Python program to search
# list in the list of lists whose
# the sum of the elements is the highest
# using the 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 ]]

prin t maximumSum (list1)

Exit :

 33 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically