Python Program to Find the List in a list of lists Whose Sum of Elements is the Highest

In this example, we will write a simple program to find the list with the highest sum of elements in the given list and print the result. To better understand this example, make sure you have knowledge of the following tutorials:-

Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
Output: [10, 11, 12]
Explanation: the 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 list 3

Python Program to Find the List in a list of lists Whose Sum of Elements is the Highest using for loop

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

index = 0
max_index = 0
sum_max = 0
for list in num:
    sum_list = 0
    for x in list:
        sum_list += x
    if sum_list > sum_max:
        sum_max = sum_list
        max_index = index
    index += 1

print(num[max_index])

The output of the above program is:-

[10, 11, 12]

Python Program to Find the List in a list of lists Whose Sum of Elements is the Highest using max function

number = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]
print(max(number, key=sum))

The output of the above program is:-

[10, 11, 12]
SHARE Python Program to Find the List in a list of lists Whose Sum of Elements is the Highest

You may also like...

Leave a Reply

Your email address will not be published.

Share