We could look at a general case where your list may have an nondetermined depth of nested lists, and the nesting may be different at different places, so forming a tree.
You can then use a recursive solution that finds the "leaves" of the tree, i.e. lists that don't contain other lists, and sum up those.
Here is how that looks:
def sum_numbers(lst):
if any(isinstance(item, list) for item in lst):
return [sum_numbers(item) if isinstance(item, list) else item
for item in lst]
else:
return sum(lst)
Here is some input data that has such irregular shape:
data = [[[1, 2, [3, 4], 5, 6]],
[[7, 8], 9, [10, [11, 12]]],
[[13, 14, 15], [16, 17, 18]],
[19, [20, 21], 22], 23, 24]
The tree returned by the function will have decreased in height, and if you feed the result back into the function, you can repeatedly reduce the tree until it becomes a single number:
print(data)
while isinstance(data, list):
data = sum_numbers(data)
print(data)
The output generated for the above example is:
[[[1, 2, [3, 4], 5, 6]], [[7, 8], 9, [10, [11, 12]]], [[13, 14, 15], [16, 17, 18]], [19, [20, 21], 22], 23, 24]
[[[1, 2, 7, 5, 6]], [15, 9, [10, 23]], [42, 51], [19, 41, 22], 23, 24]
[[21], [15, 9, 33], 93, 82, 23, 24]
[21, 57, 93, 82, 23, 24]
300