I can’t think of an easier way to do it, at least not one that doesn’t involve a lot more code…
I think you sort of understand what the logic of the problem ought to look like… but I don’t think you need quite so many returns in your function.
If you consider a very simple example, where every element in the list is simply an integer, you just want to iterate over them, test if they’re a list (which they’re not) and so simply calculate the sum.
If any given element is a list, that’s when you recursively call your function again to calculate the sum of that list.
You only need to return at the end of the function. This will serve the purpose of either handing back the final total, or handing back the sum of any sub-list found within the original set of input.
@stetim94 Hey! Thanks a lot for that viasualizer, it was extremely helpful and I’ll definitely use it in the future. Turns out the solution was a combination of loops AND recursion, as @thepitycoder suggested. Had to fight a bit but solved it!!
lst_sum = 0
for el in lst:
if isinstance(el, list):
lst_sum += sum_lst_rec(el)
lst_sum += el
test_lst_2 = [1, 2, [3, 4], [5, 6]]