Sum All Numbers in a Nested List

Medium ⏱ 12 min 48% acceptance ★★★★★ 4.9
Write deep_sum(nested) that returns the sum of all numeric values in nested, a list which may contain integers, floats, and other lists nested to any depth.

Examples

Example 1
Input
nested = [1, [2, 3], [4, [5, 6]]]
Output
21
Explanation

1+2+3+4+5+6 = 21.

Constraints

  • Nesting depth is reasonable.

Topics

ListsRecursionNested Lists

Companies

MetaDeloitte

Hints

Hint 1

Recurse whenever an element is itself a list; otherwise add it to a running total.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.