← Back to challenges

Deep List Count

PythonHardarraysrecursionalgorithms

Instructions

Create a function that takes a list and returns the number of ALL elements within it (including those within the sub-level list(s)).

Examples

deep_count([1, 2, 3]) ➞ 3

deep_count([[1], [2], [3]]) ➞ 6

deep_count(["x", "y", ["z"]]) ➞ 4

deep_count(["a", "b", ["c", "d", ["e"]]]) ➞ 7

Notes

In the examples above, notice how the sub-lists within the main list count as an element as well as the elements within that sub-list.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.