When it is required to find the total sum of a nest list using the recursion technique, a user defined method is used, that takes the list as a parameter.
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
Below is a demonstration for the same −
def recursion_sum(my_list): my_total = 0 for elem in my_list: if (type(elem) == type([])): my_total = my_total + recursion_sum(elem) else: my_total = my_total + elem return my_total my_list = [[2,3], [7,9], [11,45], [78,98]] print("The list elements are :") print(my_list) print( "The sum is :") print(recursion_sum(my_list))
The list elements are : [[2, 3], [7, 9], [11, 45], [78, 98]] The sum is : 253