{"id":9607,"date":"2021-09-30T17:30:37","date_gmt":"2021-09-30T12:00:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=9607"},"modified":"2021-11-22T18:33:32","modified_gmt":"2021-11-22T13:03:32","slug":"python-program-to-find-the-sum-of-elements-in-a-list-recursively","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-elements-in-a-list-recursively\/","title":{"rendered":"Python Program to Find the Sum of Elements in a List Recursively"},"content":{"rendered":"
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example<\/a> guide and practice well.<\/p>\n Lists in Python:<\/strong><\/p>\n A list is a Python data type that can be used in a variety of ways. A list is made up of comma-separated values, which are referred to as list items. Within square brackets, a list is declared. It’s worth noting that elements in a list don’t have to be of the same type.<\/p>\n Recursion:<\/strong><\/p>\n If you’re familiar with Python functions, you’ll know that it’s typical for one function to call another. It is also feasible for a function in Python to call itself! A recursive function calls itself, and the process of using a recursive function is known as recursion.<\/p>\n Although it may appear strange for a function to call itself, many sorts of programming challenges are better stated recursively.<\/p>\n Given a list, the task is to find the sum of the list using recursion in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Find the Sum of Elements in a List using the recursive approach in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well. Lists in Python: A list is a Python data type that can be used in a variety of ways. A list is made up of comma-separated values, which are referred to as list …<\/p>\ngiven list = 9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67<\/pre>\n
The sum of the given list\r\n [9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67] \r\nwith size 21 = 869<\/pre>\n
given list =11 13 21 85 34 96 75 73 14 25 37 39 49 47 65 69 63 21 29 123 456 789<\/pre>\n
The sum of the given list\r\n[11, 13, 21, 85, 34, 96, 75, 73, 14, 25, 37, 39, 49, 47, 65, 69, 63, 21, 29, 123, 456, 789] \r\nwith size 22 = 2234<\/pre>\n
Program to Find the Sum of Elements in a List Recursively in Python<\/h2>\n
\n
1)Using Recursion (Static Input)<\/h3>\n
\n
# function which accepts the given list and list size\r\n# as parameters and calculates the sum of the given list using recursion\r\n\r\n\r\ndef sumListRecursion(given_list, sizeOflist):\r\n # Return 0 if the list's length is zero.(base condition)\r\n if (sizeOflist == 0):\r\n return 0\r\n else:\r\n # Otherwise, along with the recursive function call,\r\n # return the sum of the last entry of the list (with the size reduced by 1).\r\n\r\n return given_list[sizeOflist-1] + sumListRecursion(given_list, sizeOflist-1)\r\n\r\n\r\n# Give the input of the list as static input and store it in a variable.\r\ngiven_list = [9, 1, 27, 48, 91, 93, 99, 27, 29,\r\n 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67]\r\n# Calculate the size of the list using len() function and store it in a variable.\r\nsizeOflist = len(given_list)\r\n# passing the given list and sizeOflist as arguments to the recursive function sumListRecursion.\r\nprint('The sum of the given list\\n', given_list, '\\nwith size ',\r\n sizeOflist, ' =', sumListRecursion(given_list, sizeOflist))\r\n<\/pre>\n
The sum of the given list\r\n [9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67] \r\nwith size 21 = 869<\/pre>\n
2)Using Recursion (User Input separated by spaces)<\/h3>\n
\n
# function which accepts the given list and list size\r\n# as parameters and calculates the sum of the given list using recursion\r\n\r\n\r\ndef sumListRecursion(given_list, sizeOflist):\r\n # Return 0 if the list's length is zero.(base condition)\r\n if (sizeOflist == 0):\r\n return 0\r\n else:\r\n # Otherwise, along with the recursive function call,\r\n # return the sum of the last entry of the list (with the size reduced by 1).\r\n\r\n return given_list[sizeOflist-1] + sumListRecursion(given_list, sizeOflist-1)\r\n\r\n\r\n# Scan the given list using map,input(),list() and split() functions.\r\ngiven_list = list(map(int, input(\r\n 'Enter some random integer elements to the list separated by spaces = ').split()))\r\n# Calculate the size of the list using len() function and store it in a variable.\r\nsizeOflist = len(given_list)\r\n# passing the given list and sizeOflist as arguments to the recursive function sumListRecursion.\r\nprint('The sum of the given list\\n', given_list, '\\nwith size ',\r\n sizeOflist, ' =', sumListRecursion(given_list, sizeOflist))\r\n<\/pre>\n
Enter some random integer elements to the list separated by spaces =\r\n 11 13 21 85 34 96 75 73 14 25 37 39 49 47 65 69 63 21 29 123 456 789\r\nThe sum of the given list\r\n[11, 13, 21, 85, 34, 96, 75, 73, 14, 25, 37, 39, 49, 47, 65, 69, 63, 21, 29, 123, 456, 789] \r\nwith size 22 = 2234<\/pre>\n
\n
\n