Python – Sum of different length Lists of list
Getting the sum of list is quite common problem and has been dealt with and discussed many times, but sometimes, we require to better it and total sum, i.e. including those of nested list as well. Let’s try and get the total sum and solve this particular problem.
Method #1 : Using list comprehension + sum() We can solve this problem using the list comprehension as a potential shorthand to the conventional loops that we may use to perform this particular task. We just iterate and sum the nested list and at end return the cumulative sum using sum function.
Python3
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ("The original list : " + str (test_list))
res = sum ([ele for sub in test_list for ele in sub])
print ("The total element sum in lists is : " + str (res))
|
Output :
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80
Time Complexity: O(n),The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Space Complexity: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method #2 : Using chain() + sum() This particular problem can also be solved using the chain function instead of list comprehension in which we use the conventional sum function to check the sum.
Python3
from itertools import chain
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ("The original list : " + str (test_list))
res = sum ( list (chain( * test_list)))
print ("The total element sum in lists is : " + str (res))
|
Output :
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80
Time Complexity: O(n) where n is the number of elements in the string list. The chain() + sum() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is required.
Method #3 : Using numpy.sum() and numpy.flatten()
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = np. sum (np.concatenate([np.array(sublist) for sublist in test_list]))
print ( "The total element sum in lists is : " + str (res))
|
Output:
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80
Time Complexity: O(n) where n is the total number of elements in the nested list
Auxiliary Space : O(n) for storing the concatenated array.
Method #4: Using reduce() from functools module
Python3
from functools import reduce
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = reduce ( lambda x,y: x + y, [ele for sub in test_list for ele in sub])
print ( "The total element sum in lists is : " + str (res))
|
Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80
Time complexity: O(N), where N is the total number of elements in all sub-lists of test_list.
Auxiliary space complexity: O(N), as we create a new list with all the elements from the sub-lists using list comprehension, and then pass it to reduce().
Method #5 : Using sum() and extend() methods
Approach
- Convert the nested list to single list using extend(),for loop and store in x
- Find the sum of x using sum() and store in res
- Display res
Python3
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.extend(i)
res = sum (x)
print ( "The total element sum in lists is : " + str (res))
|
Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80
Time Complexity : O(N) N – length of x
Auxiliary Space: O(1) since we are using single variable res to store sum
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...