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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Sum of Uneven Lists of list
# Using list comprehension + sum()
  
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using list comprehension + sum()
# Sum of Uneven Lists of list
res = sum([ele for sub in test_list for ele in sub])
  
# print result
print("The total element sum in lists is : " + str(res))

chevron_right


Output :



The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Sum of Uneven Lists of list
# Using chain() + sum()
from itertools import chain
  
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using chain() + sum()
# Sum of Uneven Lists of list
res = sum(list(chain(*test_list)))
  
# print result
print("The total element sum in lists is : " + str(res))

chevron_right


Output :

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element sum in lists is : 80


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.