Sum of list Element using Recursion

Recursive function to find out the sum of list Elements is the third recursive program recommended by the CBSE for partial fulfillment of practical examination.

Other recursive programs suggested by the CBSE are

  • Factorial of any integer number N using recursion
  • The nth Fibonacci number using recursion
  • Sum of first 100 natural number using recursion
  • Print first 100 natural number using recursion

The list is an integral part of Python programming language, lots of functions and methods are already available in Python to manipulate python lists.

Sum( ) function is already available in python to find out the sum of elements of the list item but in this program, we will find out the same using recursion.

Recursive function to find out the sum of list elements

In order to find out the sum of list elements, We are going to remove an element from the list and add the same in our stack until the list is empty.  Here is the source code

#   program to find out sum of element of a list using recursion
#   made by         : rakesh kumar

def sum_element(l):
    if(len(l) == 1):
        return l[0]
    else:
        value = l.pop()
        return value+sum_element(l)

if __name__ == "__main__":

    list1 = [1, 2, 34, 5, 6]
    result = sum_element(list1)
    print('sum of element :', result)

The output of the above program is

rakesh@folio MINGW64 /e/python (master)
$ python -u "e:\python\functions\tempCodeRunnerFile.py"
sum of element : 48

if you have any queries regarding this recursive program, please contact us via email.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.