Suppose, I have a list,
[(1,2), (3, 4)].
I will print 1 + 2 and 3 + 4 if all elements of the list are tuples. But if any one of the elements is also a list, then I add 1 to every element of the inner list and every element of that inner list is appended to the parent list. eg.
list = [(1,2), [(3, 4), (5, 6)]],
becomes
[(1, 2), (3, 4, 1), (5, 6, 1)].
Again, if the inner list has a list as an element, we repeat the same thing. eg.
[(1,2), [(3, 4), (5, 6), [(7, 8)]]]
first becomes
[(1,2), [(3, 4), (5, 6), (7, 8, 1)]]
then finally becomes,
[(1,2), (3, 4, 1), (5, 6, 1), (7, 8, 1, 1)].
How do I do this procedure to such a list, whose nesting level(as in list in a list in a list....) is not known ?
The code I used to generate this list is as follows:
def possible_sums(a):
if a == 2:
return [(1, 1)]
list_l = list(((a - 1, 1), ))
list_l.append(possible_sums(a-1))
return list_l
print(possible_sums(8))