When trying to debug a function, it can sometimes be useful to stick a print statement in there to see how it's being called. We expect that this function should eventually return because it will eventually reduce to the base case, which is an empty list. Does that happen?
def sum_list(the_list):
print(f"Summing {the_list}")
...
prints:
...
Summing [10]
Summing []
Summing [10]
Summing []
Summing [10]
Summing []
...
RecursionError: maximum recursion depth exceeded while calling a Python object
This tells us that something might be wrong with summing a list that's 1 item long. Let's step through that logic step by step:
>>> the_list = [10]
>>> mid = len(the_list) // 2
>>> the_list[:mid]
[]
>>> the_list[mid:]
[10]
So when we do our recursion on [10], we're going to end up making another recursive call to [10] again, which will just repeat infinitely. To fix this we need to extend our base case:
if len(the_list) == 1:
return the_list[0]
Note that if our base case only ever returned zero, it'd be impossible for the recursive call to ever return anything that wasn't a sum of zeroes!
sum(the_list).