I know how to get the sum of a nested list that only contains numbers but how do i get the sum of a nested list that contains both strings and numbers?
def sum_all_numbers(seq):
if not seq:
return 0
elif isinstance(seq[0], str):
seq.remove(seq[0])
elif isinstance(seq[0], list):
return sum_all_numbers(seq[0]) + sum_all_numbers(seq[1:])
else:
return seq[0] + sum_all_numbers(seq[1:])
I wanted to use double recursion to solve this problem but i don't manage to get the isinstance part right.
sum_all_numbers(["a", "b", ["c", 3, 4, [5, "d"], 10], 9])
is the input