59.6k views
0 votes
def list_length(shrinking_list): ''' A recursive way to count the number of items in a list. ''' if shrinking_list

User Shaq
by
4.7k points

1 Answer

7 votes

Answer:

Written in Python:

def list_length(shrinking_list):

if not shrinking_list:

return 0

else:

return list_length(shrinking_list[1:]) + len(shrinking_list[0])

Step-by-step explanation:

This defines the list

def list_length(shrinking_list):

This checks if list is empty, if yes it returns 0

if not shrinking_list:

return 0

If list is not empty,

else:

This calculates the length of the list, recursively

return list_length(shrinking_list[1:]) + len(shrinking_list[0])

To call the function from the main, use:

print(list_length(listname))

User Mustkeem K
by
4.8k points