Row wise sum of nested list Python
This is a Python Program to find the total sum of a nested list using recursion. Problem Description The program takes a nested list and finds the total sum of the nested list using recursion. Problem Solution 1. Initialize a variable to a nested list. advertisement Program/Source Code Here is source code of the Python Program to find the sum of a nested list using recursion. The program output is also shown below. def sum1(lst):
total = 0
for element in lst:
if (type(element) == type([])):
total = total + sum1(element)
else:
total = total + element
return total
print( "Sum is:",sum1([[1,2],[3,4]])) Program Explanation 1. A variable is initialized to a nested list. Subscribe Now: Python Programs Newsletter | Important Subjects Newsletters advertisement advertisement Runtime Test Cases
Case 1:
Sum is: 10 Sanfoundry Global Education & Learning Series Python Programs. To practice all Python programs, here is complete set of 150+ Python Problems and Solutions. Become Top Ranker in Python Programming Now! advertisement « Prev - Python Program to Check Whether a String is a Palindrome or not Using Recursion » Next - Python Program to find the factorial of a number without recursion Next Steps:
Related Posts:
|