Python Program to Find the Total Sum of a Nested List Using Recursion

Recursion in Python:

Python also supports function recursion, which means that a specified function can call itself.

Recursion is a mathematical and programming concept that is widely used. It signifies that a function calls itself. This has the advantage of allowing you to loop through data to obtain a result.

The developer must exercise extreme caution when using recursion since it is quite easy to write a function that never terminates or consumes excessive amounts of memory or computing power. However, when performed correctly, recursion may be a tremendously efficient and mathematically elegant way to programming.

Nested List:

A list can include any form of the object, including another list (sublist), which can contain sublists, and so on. This is referred to as a nested list.

Given a nested list, the task is to calculate the sum of the elements in the given list using recursion.

Examples:

Example1:

Input:

given nested list = [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]]

Output:

Printing the given nested list : 
 [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]]
The sum of all elements of the given nested list is : 966

Example2:

Input:

given Nested List = [[1, 7], [11, 28, 122], [99, 100, 11, 111]]

Output:

Printing the given nested list : 
 [[1, 7], [11, 28, 122], [99, 100, 11, 111]]
The sum of all elements of the given nested list is : 490

Program to Find the Total Sum of a Nested List Using Recursion in Python

Below is the full approach to calculate the total sum of all the elements in the given nested list using recursion in python.

Idea:

Iterate through the list, and if an element of the list is also a list, we must perform the same operation of calculating the total with this element list (which can be nested). So now that we’ve identified a subproblem, we can use the same method to complete the task by simply changing the argument to this sublist. If the element isn’t a list, just add its value to the global total.

Approach:

  • Give the nested list as static input and store it in a variable.
  • To find the total of the list’s elements, pass the list as an input to a recursive function say sumNestedListRecursion.
  • Taking a variable that stores the sum of the elements of a nested list
    and Initialize it with 0.
  • To get the elements inside the sublists, use a for loop and recursion in the function, and store the summed up elements in a variable.
  • The total sum of all the elements(integers) in the given nested list will be returned.
  • Print the total sum of all the elements in the given nested list.
  • The exit of the program.

Below is the implementation:

# function which takes the nested list as argument and
# returnsthe sum of the given nested list.


def sumnestedListRecursion(givenNestedList):
  # Taking a variable that stores the sum of the elements of a nested list
    # and Initialize it with 0.
    totalNestedSum = 0
    # To get the elements inside the sublists, use a for loop and recursion in the function,
    # and store the summed up elements in a variable.
    for eleme in givenNestedList:
        if (type(eleme) == type([])):
            totalNestedSum = totalNestedSum + sumnestedListRecursion(eleme)
        else:
            totalNestedSum = totalNestedSum + eleme
    # return the total sum of all the elements in the given nested list.
    return totalNestedSum


# Give the nested list as static input and store it in a variable.
givenNestedList = [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18],
                   [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]]
# printing the given nested list
print('Printing the given nested list : \n', givenNestedList)
# passing the given nested list as an argument to the recursive function sumnestedListRecursion
# which returns the sum of the given nested list.
print("The sum of all elements of the given nested list is :",
      sumnestedListRecursion(givenNestedList))

Output:

Printing the given nested list : 
 [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]]
The sum of all elements of the given nested list is : 966

Explanation:

  • A nested list(list of lists) is initialized in a variable.
  • To find the sum of the list’s elements, the list is passed as an argument to a recursive function.
  • A for loop and repeated recursion are used in the function to acquire the elements within the sublists.
  • The sum of the elements is calculated and returned.
  • The sum of the list of lists(nested list) elements is printed.

Related Programs: