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 761 0
Python Program to Upload Files to Your Dropbox 459 0
Python Program to Get a Function from a C Program File 252 0
Python DynamoDB Delete a Table 275 0
Python DynamoDB Scan the Table 357 0
Python DynamoDB Query the Table 278 0
Python DynamoDB Delete a Record 291 0
Python DynamoDB Update an Existing Record 282 0
Python DynamoDB Read a Record 276 0
Python DynamoDB Insert Multiple Records into Table 327 0
Python DynamoDB Insert Record into Table 287 0
Python DynamoDB Create Table 300 0
Python DynamoDB Get Started 371 0
Python Program to Test Hard Disk Speed 331 0
GUI based Python Program to Count Number of Characters 278 0
GUI based python application to count number of characters 331 3
Python MySQL prevent SQL injection in update 382 0
Python MySQL prevent SQL injection in delete 277 0
Python MySQL prevent SQL injection in select 651 0
Python MySQL join two tables 324 0
Python MySQL limit the number of records in a table 343 0
Python MySQL update existing record 286 0
Python MySQL drop table only if exist 276 0
Python MySQL drop table 280 0
Python MySQL delete record 281 0
Python MySQL sort the result in ascending order 269 0
Python MySQL sort the result in descending order 291 0
Python MySQL sort the result 255 0
Python MySQL Order By 270 0
Python MySQL wildcard select 258 0
Python MySQL select with a Filter 266 0
Python MySQL where example and usage 294 0
Python MySQL select using fetchone method 255 0
Python MySQL select particlular column from table 258 0
Python MySQL select from table 0 0
Python MySQL insert record into table and get inserted ID 278 0
Python MySQL insert multiple records into table 318 0
Python MySQL insert record into table 273 0
Python MySQL create table 272 0
Python MySQL check database exists or not 327 5
Python MySQL create database 281 11
Python MySQL connector get started 303 10
Python MongoDB Limit the Result 288 10
Python MongoDB Update Many Records 297 10
Python MongoDB Update Record 287 10
Python MongoDB Drop Collection 284 10
Python MongoDB Delete All Documents in a Collection 284 10
Python MongoDB Delete Many Documents 325 10
Python MongoDB Delete Document 305 10
Python MongoDB Sort the result in descending order 302 10
Python MongoDB Sort the Result 276 10
Python MongoDB Query Filter With Regular Expressions 321 10
Python MongoDB Query Filter the Result 295 10
Python MongoDB Select specific fields from collection 320 10
Python MongoDB Select all data from collection using find method 364 10
Python MongoDB Select data from collection using find_one method 296 10
Python MongoDB Insert Multiple Documents, with Specified IDs 300 10
Python MongoDB Insert Multiple Documents 311 10
Python MongoDB Insert Into Collection and return the _id field 302 10
Python MongoDB Insert Into Collection 297 10
Python MongoDB Check if Collection Exists 1390 10
Python MongoDB Creating a Collection 309 10
Python MongoDB Check if Database Exists 339 10
Python MongoDB Creating a Database 361 20
Python MongoDB Get Started 324 21
Python Program to Convert a given Singly Linked List to a Circular List 210 10
Python Program to Check whether a Singly Linked List is a Palindrome 818 10
Python Program to Illustrate the Operations of Singly Linked List 183 10
Python Program to Support Infinite Precision Arithmetic & Store a Number as a List of Digits 183 10
Python Program to Interchange the two Adjacent Nodes given a circular Linked List 180 10
Python Program to Interchange two Elements of the List without touching the Key Field 176 10
Python Program to Solve Josephus Problem using Linked List 573 10
Python Program to Implement Shell Sort 174 10
Python Program to Implement Comb Sort 181 10
Python Program to Implement Cocktail Shaker Sort 250 10
Python Program to Implement Gnome Sort 243 11
Python Program to Find the Sum of All Nodes in a Binary Tree 346 10
Python Program To Find the Smallest and Largest Elements in the Binary Search Tree 197 10
Python Program to Implement Priority Queue to Add and Delete Elements 165 10
Python Program to Implement Dequeue 210 10
Python Program to Flatten a List without using Recursion 169 10
Python Program to Flatten a Nested List using Recursion 289 10
Python Program to Convert a Binary Tree into a Singly Linked List by Traversing Level by Level 330 10
Python Program to Find the Total Sum of a Nested List Using Recursion 812 10
Python Program to Print the Alternate Nodes in a Linked List using Recursion 155 10
Python Program to Print the Alternate Nodes in a Linked List without using Recursion 167 10
Python Program to Implement Selection Sort 159 10
Python Program to Find Number of Occurrences of All Elements in a Linked List 218 16
Python Program to Display the Nodes of a Linked List in Reverse using Recursion 207 13
Python Program to Display the Nodes of a Linked List in Reverse without using Recursion 182 11
Python Program to Read a Linked List in Reverse 175 15
Python Program to Check String is Palindrome using Stack 224 14
Python Program to Read the Contents of a File in Reverse Order 255 20
Python Program to Count the Number of Blank Spaces in a Text File 238 16
Python Program to Read a Text File and Print all the Numbers Present in the Text File 223 12
Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File 270 27
Python Program to Count the Number of Vowels Present in a String using Sets 285 20
Python Program to Find the Second Largest Number in a List Using Bubble Sort 201 19
Python Program to Find the Length of a List Using Recursion 207 12
Python Program to Check if Expression is correctly Parenthesized 213 18
Python Program to Find the Fibonacci Series without Using Recursion 526 27
Python Program to find the factorial of a number without recursion 248 27
Python Program to Reverse a String without using Recursion 272 28
Python Program to Find the Binary Equivalent of a Number without Using Recursion 182 15
Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers without using Recursion 257 23
Python Program to Find the Sum of Digits in a Number without Recursion 234 23
Python Program to Sort the List According to the Second Element in Sublist 227 17
Python Program to Append, Delete and Display Elements of a List Using Classes 695 22
Python Program to Find the Area of a Rectangle Using Classes 226 18
Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers 302 31
Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it 209 15
Python Program to Create a Class which Performs Basic Calculator Operations 226 17
Python Program to Create a Class and Compute the Area and the Perimeter of the Circle 230 19
Python Program that Displays which Letters are in the First String but not in the Second 220 19
Python Program to Sort a List According to the Length of the Elements 244 18
Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List 564 24
Python Program to Find the Sum of the Digits of the Number Recursively 245 21
Python Program to Find the Binary Equivalent of a Number Recursively 234 20
Python Program to Find the Sum of Elements in a List Recursively 217 19
Python Program to Find the Factorial of a Number Using Recursion 165 19
Python Program to Find the Fibonacci Series Using Recursion 259 28
Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively 286 12
Python Program to Determine Whether a Given Number is Even or Odd Recursively 199 14
Python Program to Put Even and Odd elements in a List into Two Different Lists 338 20
Python Program to Find the Second Largest Number in a List 188 16
Python Program to Find the Largest Number in a List 266 22
Python Program to Clear the Rightmost Set Bit of a Number 272 22
Python Program to Count Set Bits in a Number 178 15
Python Program to test Collatz Conjecture for a Given Number 258 18
Python Program to Search the Number of Times a Particular Number Occurs in a List 186 14
Python Program to Determine all Pythagorean Triplets in the Range 204 18
Python Program to Find the Sum of First N Natural Numbers 219 19
Python Program to Find the Sum of Cosine Series 503 18
Python Program to Find the Sum of Sine Series 716 20
Python Program to Check If Two Numbers are Amicable Numbers 182 17
Python Program to Find the LCM of Two Numbers 257 31
Python Program to Check if a Number is a Strong Number 185 20
Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers 183 17
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List 233 11
Python Program to Compute Prime Factors of an Integer 261 28
Python Program to Check if a Date is Valid and Print the Incremented Date if it is 450 22
Python Program to Print an Inverted Star Pattern 325 20
Python Program to Implement Binary Insertion Sort 179 13
Python Program to Implement Insertion Sort 191 20
Python Program to Implement Binary Search with Recursion 165 19
Python Program to Implement Binary Search without Recursion 237 22
Python Program to Implement Linear Search 225 24
Python Program to Sort using a Binary Search Tree 219 18
Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List 264 26
Python Program to Print an Identity Matrix 178 16
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern 233 26
Python Program to Read a Number n And Print the Series 1+2+.....+n= 239 28
Python Program to Print Odd Numbers Within a Given Range 217 26
Python Program to Accept Three Digits and Print all Possible Combinations from the Digits 336 19
Python Program to Read Two Numbers and Print Their Quotient and Remainder 356 30
Python Program to solve Maximum Subarray Problem using Kadanes Algorithm 294 18
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable 195 15
Python Program to Find Element Occurring Odd Number of Times in a List 174 20
Python Program to Replace all Occurrences of a with a $ in a String 200 17
Python Program to Remove the nth Index Character from a Non-Empty String 386 16
Python Program to Check if a Substring is Present in a Given String 238 20
Python Program to Count the Occurrences of Each Word in a Given String Sentence 251 19
Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String 200 15
Python Program to Calculate the Number of Digits and Letters in a String 305 13
Python Program to Check if a String is a Pangram or Not 256 20
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a String 927 21
Python Program to Check if a String is a Palindrome or Not 172 11
Python Program to Count Number of Lowercase Characters in a String 199 19
Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions 223 18
Python Program to Calculate the Number of Words and the Number of Characters Present in a String 207 19
Python Program to Remove the Characters of Odd Index Values in a String 517 23
Python Program to Calculate the Length of a String Without Using a Library Function 204 21
Python Program to Take in a String and Replace Every Blank Space with Hyphen 267 25
Python Program to Count the Number of Vowels in a String 198 23
Python Program to Detect if Two Strings are Anagrams 230 22
Python Program to Form a New String where the First Character and the Last Character have been Exchanged 387 12

Comments