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










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



Description:
For this program, a recursive function: "add" is used which takes a list as a parameter.
While iterating over the elements of the list, we check if the element is of type list and recursively call the add function on the element, else the element is of type int and it is added to the total variable.
Finally, the value of the total variable is returned.

Python Code:
class Sum: def add(self, nlist): total = 0 for element in nlist: if type(element) == list: total += self.add(element) else: total += element return total def main(): nested_list = [1, 2, [3, 4, [5, 6]], 7, 8, [9, [10]]] obj = Sum() total = obj.add(nested_list) print("Total Sum:", total) if __name__ == "__main__": main()
Output:
Total Sum: 55

More Articles of Siddhartha Dutta:

Name Views Likes
Python Program to Upload Files to Your Google Drive 447 0
Python Program to Upload Files to Your Dropbox 268 0
Python Program to Get a Function from a C Program File 154 0
Python DynamoDB Delete a Table 175 0
Python DynamoDB Scan the Table 196 0
Python DynamoDB Query the Table 178 0
Python DynamoDB Delete a Record 173 0
Python DynamoDB Update an Existing Record 177 0
Python DynamoDB Read a Record 166 0
Python DynamoDB Insert Multiple Records into Table 170 0
Python DynamoDB Insert Record into Table 179 0
Python DynamoDB Create Table 188 0
Python DynamoDB Get Started 240 0
Python Program to Test Hard Disk Speed 197 0
GUI based Python Program to Count Number of Characters 180 0
GUI based python application to count number of characters 232 3
Python MySQL prevent SQL injection in update 242 0
Python MySQL prevent SQL injection in delete 165 0
Python MySQL prevent SQL injection in select 391 0
Python MySQL join two tables 217 0
Python MySQL limit the number of records in a table 214 0
Python MySQL update existing record 188 0
Python MySQL drop table only if exist 178 0
Python MySQL drop table 174 0
Python MySQL delete record 167 0
Python MySQL sort the result in ascending order 168 0
Python MySQL sort the result in descending order 186 0
Python MySQL sort the result 154 0
Python MySQL Order By 159 0
Python MySQL wildcard select 168 0
Python MySQL select with a Filter 163 0
Python MySQL where example and usage 169 0
Python MySQL select using fetchone method 157 0
Python MySQL select particlular column from table 166 0
Python MySQL select from table 0 0
Python MySQL insert record into table and get inserted ID 177 0
Python MySQL insert multiple records into table 210 0
Python MySQL insert record into table 173 0
Python MySQL create table 177 0
Python MySQL check database exists or not 194 5
Python MySQL create database 183 11
Python MySQL connector get started 194 10
Python MongoDB Limit the Result 188 10
Python MongoDB Update Many Records 189 10
Python MongoDB Update Record 191 10
Python MongoDB Drop Collection 188 10
Python MongoDB Delete All Documents in a Collection 188 10
Python MongoDB Delete Many Documents 205 10
Python MongoDB Delete Document 197 10
Python MongoDB Sort the result in descending order 198 10
Python MongoDB Sort the Result 177 10
Python MongoDB Query Filter With Regular Expressions 200 10
Python MongoDB Query Filter the Result 181 10
Python MongoDB Select specific fields from collection 210 10
Python MongoDB Select all data from collection using find method 228 10
Python MongoDB Select data from collection using find_one method 198 10
Python MongoDB Insert Multiple Documents, with Specified IDs 202 10
Python MongoDB Insert Multiple Documents 214 10
Python MongoDB Insert Into Collection and return the _id field 202 10
Python MongoDB Insert Into Collection 195 10
Python MongoDB Check if Collection Exists 724 10
Python MongoDB Creating a Collection 204 10
Python MongoDB Check if Database Exists 214 10
Python MongoDB Creating a Database 256 20
Python MongoDB Get Started 232 21
Python Program to Convert a given Singly Linked List to a Circular List 170 10
Python Program to Check whether a Singly Linked List is a Palindrome 477 10
Python Program to Illustrate the Operations of Singly Linked List 146 10
Python Program to Support Infinite Precision Arithmetic & Store a Number as a List of Digits 151 10
Python Program to Interchange the two Adjacent Nodes given a circular Linked List 153 10
Python Program to Interchange two Elements of the List without touching the Key Field 147 10
Python Program to Solve Josephus Problem using Linked List 250 10
Python Program to Implement Shell Sort 140 10
Python Program to Implement Comb Sort 145 10
Python Program to Implement Cocktail Shaker Sort 165 10
Python Program to Implement Gnome Sort 174 11
Python Program to Find the Sum of All Nodes in a Binary Tree 226 10
Python Program To Find the Smallest and Largest Elements in the Binary Search Tree 164 10
Python Program to Implement Priority Queue to Add and Delete Elements 137 10
Python Program to Implement Dequeue 142 10
Python Program to Flatten a List without using Recursion 139 10
Python Program to Flatten a Nested List using Recursion 198 10
Python Program to Convert a Binary Tree into a Singly Linked List by Traversing Level by Level 259 10
Python Program to Find the Total Sum of a Nested List Using Recursion 484 10
Python Program to Print the Alternate Nodes in a Linked List using Recursion 123 10
Python Program to Print the Alternate Nodes in a Linked List without using Recursion 130 10
Python Program to Implement Selection Sort 128 10
Python Program to Find Number of Occurrences of All Elements in a Linked List 186 16
Python Program to Display the Nodes of a Linked List in Reverse using Recursion 160 13
Python Program to Display the Nodes of a Linked List in Reverse without using Recursion 145 11
Python Program to Read a Linked List in Reverse 141 15
Python Program to Check String is Palindrome using Stack 181 14
Python Program to Read the Contents of a File in Reverse Order 204 20
Python Program to Count the Number of Blank Spaces in a Text File 201 16
Python Program to Read a Text File and Print all the Numbers Present in the Text File 161 12
Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File 233 27
Python Program to Count the Number of Vowels Present in a String using Sets 248 20
Python Program to Find the Second Largest Number in a List Using Bubble Sort 164 19
Python Program to Find the Length of a List Using Recursion 177 12
Python Program to Check if Expression is correctly Parenthesized 178 18
Python Program to Find the Fibonacci Series without Using Recursion 321 27
Python Program to find the factorial of a number without recursion 212 27
Python Program to Reverse a String without using Recursion 238 28
Python Program to Find the Binary Equivalent of a Number without Using Recursion 156 15
Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers without using Recursion 214 23
Python Program to Find the Sum of Digits in a Number without Recursion 197 23
Python Program to Sort the List According to the Second Element in Sublist 190 17
Python Program to Append, Delete and Display Elements of a List Using Classes 472 22
Python Program to Find the Area of a Rectangle Using Classes 192 18
Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers 259 31
Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it 142 15
Python Program to Create a Class which Performs Basic Calculator Operations 172 17
Python Program to Create a Class and Compute the Area and the Perimeter of the Circle 190 19
Python Program that Displays which Letters are in the First String but not in the Second 186 19
Python Program to Sort a List According to the Length of the Elements 193 18
Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List 328 24
Python Program to Find the Sum of the Digits of the Number Recursively 213 21
Python Program to Find the Binary Equivalent of a Number Recursively 196 20
Python Program to Find the Sum of Elements in a List Recursively 194 19
Python Program to Find the Factorial of a Number Using Recursion 132 19
Python Program to Find the Fibonacci Series Using Recursion 223 28
Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively 223 12
Python Program to Determine Whether a Given Number is Even or Odd Recursively 162 14
Python Program to Put Even and Odd elements in a List into Two Different Lists 281 20
Python Program to Find the Second Largest Number in a List 160 16
Python Program to Find the Largest Number in a List 225 22
Python Program to Clear the Rightmost Set Bit of a Number 228 22
Python Program to Count Set Bits in a Number 149 15
Python Program to test Collatz Conjecture for a Given Number 174 18
Python Program to Search the Number of Times a Particular Number Occurs in a List 163 14
Python Program to Determine all Pythagorean Triplets in the Range 170 18
Python Program to Find the Sum of First N Natural Numbers 182 19
Python Program to Find the Sum of Cosine Series 399 18
Python Program to Find the Sum of Sine Series 480 20
Python Program to Check If Two Numbers are Amicable Numbers 154 17
Python Program to Find the LCM of Two Numbers 214 31
Python Program to Check if a Number is a Strong Number 159 20
Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers 141 17
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List 202 11
Python Program to Compute Prime Factors of an Integer 222 28
Python Program to Check if a Date is Valid and Print the Incremented Date if it is 247 22
Python Program to Print an Inverted Star Pattern 235 20
Python Program to Implement Binary Insertion Sort 145 13
Python Program to Implement Insertion Sort 157 20
Python Program to Implement Binary Search with Recursion 133 19
Python Program to Implement Binary Search without Recursion 209 22
Python Program to Implement Linear Search 198 24
Python Program to Sort using a Binary Search Tree 184 18
Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List 222 26
Python Program to Print an Identity Matrix 144 16
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern 201 26
Python Program to Read a Number n And Print the Series 1+2+.....+n= 207 28
Python Program to Print Odd Numbers Within a Given Range 180 26
Python Program to Accept Three Digits and Print all Possible Combinations from the Digits 263 19
Python Program to Read Two Numbers and Print Their Quotient and Remainder 232 30
Python Program to solve Maximum Subarray Problem using Kadanes Algorithm 243 18
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable 167 15
Python Program to Find Element Occurring Odd Number of Times in a List 147 20
Python Program to Replace all Occurrences of a with a $ in a String 165 17
Python Program to Remove the nth Index Character from a Non-Empty String 289 16
Python Program to Check if a Substring is Present in a Given String 202 20
Python Program to Count the Occurrences of Each Word in a Given String Sentence 212 19
Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String 170 15
Python Program to Calculate the Number of Digits and Letters in a String 251 13
Python Program to Check if a String is a Pangram or Not 229 20
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a String 323 21
Python Program to Check if a String is a Palindrome or Not 146 11
Python Program to Count Number of Lowercase Characters in a String 163 19
Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions 196 18
Python Program to Calculate the Number of Words and the Number of Characters Present in a String 181 19
Python Program to Remove the Characters of Odd Index Values in a String 328 23
Python Program to Calculate the Length of a String Without Using a Library Function 169 21
Python Program to Take in a String and Replace Every Blank Space with Hyphen 224 25
Python Program to Count the Number of Vowels in a String 166 23
Python Program to Detect if Two Strings are Anagrams 198 22
Python Program to Form a New String where the First Character and the Last Character have been Exchanged 272 12

Comments