# Python – Skew Nested Tuple Summation

Given a Tuple, nesting at 2nd position, return summation of 1st elements.

Input : test_tup = (5, (6, (1, (9, None))))
Output : 21
Explanation : 9 + 6 + 5 + 1 = 21.

Input : test_tup = (5, (6, (1, None)))
Output : 12
Explanation : 1 + 6 + 5 = 12.

Method #1 : Using infinite loop

In this, we perform get into skew structure while summation using infinite loop and break when we reach None value.

## Python3

 `# Python3 code to demonstrate working of  ` `# Skew Nested Tuple Summation ` `# Using infinite loop ` ` `  `# initializing tuple ` `test_tup ``=` `(``5``, (``6``, (``1``, (``9``, (``10``, ``None``))))) ` ` `  `# printing original tuple ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `res ``=` `0` `while` `test_tup: ` `    ``res ``+``=` `test_tup[``0``] ` `     `  `    ``# assigning inner tuple as original ` `    ``test_tup ``=` `test_tup[``1``] ` ` `  `# printing result  ` `print``(``"Summation of 1st positions : "` `+` `str``(res))  `

Output

```The original tuple is : (5, (6, (1, (9, (10, None)))))
Summation of 1st positions : 31

```

Method #2 : Using recursion

In this, we perform summation and recur for the 2nd element of tuple,  return on None.

## Python3

 `# Python3 code to demonstrate working of  ` `# Skew Nested Tuple Summation ` `# Using recursion ` ` `  `# helper function to perform task ` `def` `tup_sum(test_tup): ` `     `  `    ``# return on None  ` `    ``if` `not` `test_tup: ` `        ``return` `0` `    ``else``: ` `        ``return` `test_tup[``0``] ``+` `tup_sum(test_tup[``1``]) ` ` `  `# initializing tuple ` `test_tup ``=` `(``5``, (``6``, (``1``, (``9``, (``10``, ``None``))))) ` ` `  `# printing original tuple ` `print``(``"The original tuple is : "` `+` `str``(test_tup)) ` ` `  `# calling fnc. ` `res ``=` `tup_sum(test_tup) ` ` `  `# printing result  ` `print``(``"Summation of 1st positions : "` `+` `str``(res))  `

Output

```The original tuple is : (5, (6, (1, (9, (10, None)))))
Summation of 1st positions : 31

```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.