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 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.
Recursion:
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.
Although it may appear strange for a function to call itself, many sorts of programming challenges are better stated recursively.
Given a list, the task is to find the sum of the list using recursion in Python.
- Python Program to Find the Power of a Number Using Recursion
- Python Program to Reverse a String Using Recursion
- Python Program to Count Number of Digits in a Number using Recursion
Examples:
Example1:
Input:
given list = 9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67
Output:
The sum of the given list [9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67] with size 21 = 869
Example2:
Input:
given list =11 13 21 85 34 96 75 73 14 25 37 39 49 47 65 69 63 21 29 123 456 789
Output:
The sum of the given list [11, 13, 21, 85, 34, 96, 75, 73, 14, 25, 37, 39, 49, 47, 65, 69, 63, 21, 29, 123, 456, 789] with size 22 = 2234
Program to Find the Sum of Elements in a List Recursively in Python
Below are the ways to Find the Sum of Elements in a List using the recursive approach in Python:
1)Using Recursion (Static Input)
Approach:
- Give the input of the list as static input and store it in a variable.
- Calculate the size of the list using the len() function and store it in a variable.
- Create a recursive function that accepts an array and its size as parameters.
- The recursive function will take the list and its size as inputs.
- Return 0 if the list’s length is zero.
- Otherwise, along with the recursive function call, return the sum of the last entry of the list (with the size reduced by 1).
- The final sum is printed when the returned result is saved in a variable.
- The exit of the Program.
Below is the implementation:
# function which accepts the given list and list size # as parameters and calculates the sum of the given list using recursion def sumListRecursion(given_list, sizeOflist): # Return 0 if the list's length is zero.(base condition) if (sizeOflist == 0): return 0 else: # Otherwise, along with the recursive function call, # return the sum of the last entry of the list (with the size reduced by 1). return given_list[sizeOflist-1] + sumListRecursion(given_list, sizeOflist-1) # Give the input of the list as static input and store it in a variable. given_list = [9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67] # Calculate the size of the list using len() function and store it in a variable. sizeOflist = len(given_list) # passing the given list and sizeOflist as arguments to the recursive function sumListRecursion. print('The sum of the given list\n', given_list, '\nwith size ', sizeOflist, ' =', sumListRecursion(given_list, sizeOflist))
Output:
The sum of the given list [9, 1, 27, 48, 91, 93, 99, 27, 29, 33, 39, 19, 11, 27, 29, 35, 39, 12, 65, 69, 67] with size 21 = 869
2)Using Recursion (User Input separated by spaces)
Approach:
- Scan the given list using map,input(),list() and split() functions.
- Calculate the size of the list using the len() function and store it in a variable.
- Create a recursive function that accepts an array and its size as parameters.
- The recursive function will take the list and its size as inputs.
- Return 0 if the list’s length is zero.
- Otherwise, along with the recursive function call, return the sum of the last entry of the list (with the size reduced by 1).
- The final sum is printed when the returned result is saved in a variable.
- The exit of the Program.
Below is the implementation:
# function which accepts the given list and list size # as parameters and calculates the sum of the given list using recursion def sumListRecursion(given_list, sizeOflist): # Return 0 if the list's length is zero.(base condition) if (sizeOflist == 0): return 0 else: # Otherwise, along with the recursive function call, # return the sum of the last entry of the list (with the size reduced by 1). return given_list[sizeOflist-1] + sumListRecursion(given_list, sizeOflist-1) # Scan the given list using map,input(),list() and split() functions. given_list = list(map(int, input( 'Enter some random integer elements to the list separated by spaces = ').split())) # Calculate the size of the list using len() function and store it in a variable. sizeOflist = len(given_list) # passing the given list and sizeOflist as arguments to the recursive function sumListRecursion. print('The sum of the given list\n', given_list, '\nwith size ', sizeOflist, ' =', sumListRecursion(given_list, sizeOflist))
Output:
Enter some random integer elements to the list separated by spaces = 11 13 21 85 34 96 75 73 14 25 37 39 49 47 65 69 63 21 29 123 456 789 The sum of the given list [11, 13, 21, 85, 34, 96, 75, 73, 14, 25, 37, 39, 49, 47, 65, 69, 63, 21, 29, 123, 456, 789] with size 22 = 2234
Explanation:
- A recursive function is called using the list and its size as parameters.
- If the function’s size reduces to zero, 0 is returned.
- Otherwise, the recursive function call (with the size reduced by 1) is returned, along with the sum of the last entry of the list.
- The returned values are saved in a list, and the sum of the list’s items is printed using the print() function.
Related Programs:
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Find the Power of a Number Using Recursion
- Python Program to Find the LCM of Two Numbers Using Recursion
- Python Program to Find the Length of a List Using Recursion
- Python Program to Reverse a String Using Recursion