python - What would be the non-comprehension list way of doing this sum -
def nested_sum(l): return sum( nested_sum(x) if isinstance(x, list) else x x in l )
this solution given eugenie in following post: sum of nested list in python
i trying recreate without using comprehension list, i'm not able it. how it?
the code uses generator expression, not list comprehension.
use loop , +=
adding results:
def nested_sum(l): total = 0 x in l: total += nested_sum(x) if isinstance(x, list) else x return total
or, if want conditional expression expanded if
statement well:
def nested_sum(l): total = 0 x in l: if isinstance(x, list): total += nested_sum(x) else: total += x return total
Comments
Post a Comment