>>> x = [1, 2, [3, 4, 5, [6, 7], [8, [9, [10, 11], 12]], 13, 14], 15]
>>> from collections import Iterable
>>> def nested_to_flat(items):
for i in items:
if isinstance(i, Iterable):
yield from nested_to_flat(i)
else:
yield i
>>> list(nested_to_flat(x))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
def rec_for_sum(lst):
if not lst:
return 0
count = 0
for el in lst:
if not isinstance(el, list):
count += el
else:
count += rec_for_sum(el)
return count