Ahh recursion, tends to make things more complex than they need to be, but it's still cool!
Let's add some debug code to see what is happening
def list_length(my_lis):
if my_lis:
print(my_lis)
length = list_length(my_lis[1:])
print(str(my_lis) + " returning 1 + " + str(length))
return 1 + length
print("Empty List! Return 0")
return 0
test_list = ['1', '2', '3', '4']
answer = list_length(test_list)
print("Answer: " + str(answer))
This will output:
['1', '2', '3', '4']
['2', '3', '4']
['3', '4']
['4']
Empty List! Return 0
['4'] returning 1 + 0
['3', '4'] returning 1 + 1
['2', '3', '4'] returning 1 + 2
['1', '2', '3', '4'] returning 1 + 3
Answer: 4
Notice how we only reach the base case (return 0) after the list is empty. Also notice that the number doesn't start going up until AFTER we hit the base case. Only after we reach an empty list can we start going back up the chain and counting 1 + whatever the last function got.
list_length(my_lis[1:])should give the length of that list, right? Now, compared to the original list, how long is that? If you add 1 to that result, what do you get? What exactly is the conceptual difficulty here?