3 ms·
Your function is fine but if you didn't want it to affect the original list you could do something like: def sum(lst): if lst: return lst
by sleepyams 11y ago
Your function is fine but if you didn't want it to affect the original list you could do something like:
def sum(lst):
if lst:
return lst[0] + sum(lst[1:])
return 0
- jakeogh 11y agoWithout knowing about the recursion limit that's what I should have written if I understood .pop() was mutating the input var's global state. It's a nice accident though.