This is a Python Program to find the length of a list recursively.
The program takes a list and finds the length of the list recursively.
1. Define a recursive function which takes a list as the argument.
2. Initialize a variable to a list.
3. In the function, put the condition that if it is not the original list, return 0.
4. Otherwise, recursively call the function to find the length of the list.
5. Print the final result of the string.
6. Exit.
Here is source code of the Python Program to find the length of a list recursively. The program output is also shown below.
def length(lst): if not lst: return 0 return 1 + length(lst[1::2]) + length(lst[2::2]) a=[1,2,3] print("Length of the string is: ") print(a)
1. Define a recursive function which takes a list as the argument.
2. Initialize a variable to a list.
3. In the function, put the condition that if it is not the original list, return 0.
4. Otherwise, recursively call the function to find the length of the list.
5. Print the final result of the string.
Case 1: Length of the string is: [1, 2, 3]
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Information Technology Books
- Apply for Python Internship
- Check Python Books
- Practice Programming MCQs
- Apply for Programming Internship