Python Maximum sum of elements of list in a list of lists (original) (raw)
Last Updated : 15 Jan, 2025
In this problem, we need to find the maximum sum of elements in a list of lists. A list of lists is a collection of sub-lists and each sub-list contains individual elements. The problem is to find the maximum sum among all the sub-lists. Let's explore various ways to solve this problem.
**Using sum() with max()
The simplest and most efficient way is by using the sum() function to calculate the sum of elements in each sub-list and then using the max() function to find the maximum sum.
Python `
Input list of lists
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Find the maximum sum of elements in the sub-lists
res = max(sum(sub_lst) for sub_lst in lst) print(res)
`
Explanation:
- sum() function is used to calculate the sum of elements in each sub-list.
- max() function is then used to find the sub-list with the highest sum.
Let's explore some more ways and see how we can find the maximum sum of elements of list in a list of lists.
Table of Content
**Using for loop
We can also use a for loop to iterate through each sub-list, calculate the sum of each sub-list and then keep track of the maximum sum encountered during the iteration.
Python `
Input list of lists
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Initialize the maximum sum as a very small number
max_sum = float('-inf')
Loop through each sub-list and calculate the sum
for b in a: cur_sum = sum(b) max_sum = max(max_sum, cur_sum)
print(max_sum)
`
Explanation:
- We use a for loop to calculate the sum of each sub-list using the sum() function.
- We keep track of the maximum sum encountered so far using the max() function.
**Using list comprehension and max()
This method is similar to the first one, but we will use list comprehension to create a list of sums and then apply the max() function to find the maximum sum.
Python `
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Create a list of sums and find the maximum sum
res = max([sum(sub_lst) for sub_lst in a]) print(res)
`
Explanation:
- List comprehension is used to create a list of sums for each sub-list.
- max() function is then used to find the maximum sum from the list of sums.
**Using numpy for large lists
If we are working with large lists, the numpy library can provide an efficient solution for this problem. Using numpy's sum() function, we can find the maximum sum of elements in the list of lists.
Python `
import numpy as np a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Convert the list of lists to a numpy array
arr = np.array(a)
Find the maximum sum of elements in a row (sub-list)
res = np.max(np.sum(arr, axis=1)) print(res)
`
Explanation:
- We first convert the list of lists into a numpy array.
- The np.sum() function calculates the sum of each row (sub-list) and np.max() finds the maximum sum.
Similar Reads
- Maximum and Minimum element in a Set in Python We are given a set and our task is to find the maximum and minimum elements from the given set. For example, if we have a = {3, 1, 7, 5, 9}, the maximum element should be 9, and the minimum element should be 1. Let’s explore different methods to do it: 1. Using min() & max() functionThe built-in 3 min read
- List Methods in Python | Set 1 (in, not in, len(), min(), max()...) List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3 2 min read
- Python | Find frequency of largest element in list Given a list, the task is to find the number of occurrences of the largest element of the list.Examples: Input : [1, 2, 8, 5, 8, 7, 8] Output :3 Input : [2, 9, 1, 3, 4, 5] Output :1 Method 1: The naive approach is to find the largest element present in the list using max(list) function, then iterati 2 min read
- Subtract two list elements if element in first list is greater - Python We are given two lists of equal length. Our task is to subtract the corresponding elements of these lists, but only if the element from the first list is greater than the corresponding element from the second list. If the element from the first list is smaller or equal, we leave the result as zero o 3 min read
- Longest alternating subsequence which has maximum sum of elements Given a list of length N with positive and negative integers. The task is to choose the longest alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite of the sign of the current element). Among all such subsequences, we have to choose one which has the maxi 10 min read
- Use of min() and max() in Python Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively, 2 min read
- Python | Max/Min value in Nth Column in Matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed 7 min read
- Element with Largest Frequency in List We are given a list we need to find the element with largest frequency in a list . For example, we are having a list a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to find the element with most frequency which will be 4 in this case. Using collections.CounterCounter is a convenient way to count elements 3 min read
- Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu 4 min read
- Ways to Iterate Tuple List of Lists - Python In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi 3 min read