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 – 1000 Python Programs.
If you wish to look at all Python Programming examples, go to 1000 Python Programs.
- Practice Programming MCQs
- Check Python Books
- Check Information Technology Books
- Apply for Programming Internship
- Apply for Python Internship